aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/core/OsgiLogHandler.java
blob: 989bc26dd855407b68f843ee694a40f5cd599d8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;

import com.google.common.collect.ImmutableMap;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogLevel;
import org.osgi.service.log.LogService;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * @author Simon Thoresen Hult
 */
class OsgiLogHandler extends Handler {

    private enum LogRecordProperty {

        LEVEL,
        LOGGER_NAME,
        MESSAGE,
        MILLIS,
        PARAMETERS,
        RESOURCE_BUNDLE,
        RESOURCE_BUNDLE_NAME,
        SEQUENCE_NUMBER,
        SOURCE_CLASS_NAME,
        SOURCE_METHOD_NAME,
        THREAD_ID,
        THROWN

    }

    private final static Map<String, LogRecordProperty> PROPERTY_MAP = createDictionary(LogRecordProperty.values());
    private final static String[] PROPERTY_KEYS = toStringArray(LogRecordProperty.values());
    private final LogService logService;

    public OsgiLogHandler(LogService logService) {
        this.logService = logService;
    }

    @Override
    @SuppressWarnings("deprecation")
    public void publish(LogRecord record) {
        logService.log(new LogRecordReference(record), toServiceLevel(record.getLevel()).ordinal(), record.getMessage(),
                       record.getThrown());
    }

    @Override
    public void flush() {
        // empty
    }

    @Override
    public void close() {
        // empty
    }

    public static LogLevel toServiceLevel(Level level) {
        int val = level.intValue();
        if (val >= Level.SEVERE.intValue()) {
            return LogLevel.ERROR;
        }
        if (val >= Level.WARNING.intValue()) {
            return LogLevel.WARN;
        }
        if (val >= Level.INFO.intValue()) {
            return LogLevel.INFO;
        }
        // Level.CONFIG
        // Level.FINE
        // Level.FINER
        // Level.FINEST
        return LogLevel.DEBUG;
    }

    private static <T> Map<String, T> createDictionary(T[] in) {
        Map<String, T> out = new HashMap<>();
        for (T t : in) {
            out.put(String.valueOf(t), t);
        }
        return ImmutableMap.copyOf(out);
    }

    private static String[] toStringArray(Object[] in) {
        String[] out = new String[in.length];
        for (int i = 0; i < in.length; ++i) {
            out[i] = String.valueOf(in[i]);
        }
        return out;
    }

    private static class LogRecordReference implements ServiceReference<LogRecord> {

        final LogRecord record;

        LogRecordReference(LogRecord record) {
            this.record = record;
        }

        @Override
        public Object getProperty(String s) {
            LogRecordProperty property = PROPERTY_MAP.get(s);
            if (property == null) {
                return null;
            }
            switch (property) {
            case LEVEL:
                return record.getLevel();
            case LOGGER_NAME:
                return record.getLoggerName();
            case MESSAGE:
                return record.getMessage();
            case MILLIS:
                return record.getMillis();
            case PARAMETERS:
                return record.getParameters();
            case RESOURCE_BUNDLE:
                return record.getResourceBundle();
            case RESOURCE_BUNDLE_NAME:
                return record.getResourceBundleName();
            case SEQUENCE_NUMBER:
                return record.getSequenceNumber();
            case SOURCE_CLASS_NAME:
                return record.getSourceClassName();
            case SOURCE_METHOD_NAME:
                return record.getSourceMethodName();
            case THREAD_ID:
                return record.getLongThreadID();
            case THROWN:
                return record.getThrown();
            default:
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public String[] getPropertyKeys() {
            return PROPERTY_KEYS;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle[] getUsingBundles() {
            return new Bundle[0];
        }

        @Override
        public boolean isAssignableTo(Bundle bundle, String s) {
            return false;
        }

        @Override
        public int compareTo(Object o) {
            return 0;
        }

        @Override
        public Dictionary<String, Object> getProperties() {
            return new Hashtable<>();
        }

        @Override
        public <A> A adapt(Class<A> aClass) { return null; }

    }
}