|
1 |
| -package ch.qos.logback.classic.encoder; |
2 |
| - |
3 |
| -import ch.qos.logback.classic.spi.ILoggingEvent; |
4 |
| -import ch.qos.logback.core.encoder.JsonEncoderBase; |
5 |
| -import ch.qos.logback.core.util.DirectJson; |
6 |
| - |
7 |
| -import java.util.ArrayList; |
8 |
| -import java.util.List; |
9 |
| - |
10 |
| -/** |
11 |
| - * This is a concrete JsonEncoder for {@link ILoggingEvent} that emits fields according to object's configuration. |
12 |
| - * It is partially imported from <a href="https://github.com/hkupty/penna">penna</a>, but adapted to logback's structure. |
13 |
| - * |
14 |
| - * @author Henry John Kupty |
15 |
| - */ |
16 |
| -public class JsonEncoder extends JsonEncoderBase<ILoggingEvent> { |
17 |
| - |
18 |
| - |
19 |
| - // Excerpt below imported from |
20 |
| - // ch.qos.logback.contrib.json.classic.JsonLayout |
21 |
| - public static final String TIMESTAMP_ATTR_NAME = "timestamp"; |
22 |
| - public static final String LEVEL_ATTR_NAME = "level"; |
23 |
| - public static final String MARKERS_ATTR_NAME = "tags"; |
24 |
| - public static final String THREAD_ATTR_NAME = "thread"; |
25 |
| - public static final String MDC_ATTR_NAME = "mdc"; |
26 |
| - public static final String LOGGER_ATTR_NAME = "logger"; |
27 |
| - public static final String FORMATTED_MESSAGE_ATTR_NAME = "message"; |
28 |
| - public static final String MESSAGE_ATTR_NAME = "raw-message"; |
29 |
| - public static final String EXCEPTION_ATTR_NAME = "exception"; |
30 |
| - public static final String CONTEXT_ATTR_NAME = "context"; |
31 |
| - |
32 |
| - protected boolean includeLevel; |
33 |
| - protected boolean includeThreadName; |
34 |
| - protected boolean includeMDC; |
35 |
| - protected boolean includeLoggerName; |
36 |
| - protected boolean includeFormattedMessage; |
37 |
| - protected boolean includeMessage; |
38 |
| - protected boolean includeException; |
39 |
| - protected boolean includeContextName; |
40 |
| - |
41 |
| - private final List<Emitter<ILoggingEvent>> emitters; |
42 |
| - |
43 |
| - |
44 |
| - public JsonEncoder() { |
45 |
| - super(); |
46 |
| - |
47 |
| - emitters = new ArrayList<>(); |
48 |
| - this.includeLevel = true; |
49 |
| - this.includeThreadName = true; |
50 |
| - this.includeMDC = true; |
51 |
| - this.includeLoggerName = true; |
52 |
| - this.includeFormattedMessage = true; |
53 |
| - this.includeException = true; |
54 |
| - this.includeContextName = true; |
55 |
| - } |
56 |
| - |
57 |
| - //protected = new DirectJson(); |
58 |
| - |
59 |
| - |
60 |
| - public void writeMessage(DirectJson jsonWriter, ILoggingEvent event) { |
61 |
| - jsonWriter.writeStringValue(MESSAGE_ATTR_NAME, event.getMessage()); |
62 |
| - } |
63 |
| - |
64 |
| - public void writeFormattedMessage(DirectJson jsonWriter, ILoggingEvent event) { |
65 |
| - jsonWriter.writeStringValue(FORMATTED_MESSAGE_ATTR_NAME, event.getFormattedMessage()); |
66 |
| - } |
67 |
| - |
68 |
| - public void writeLogger(DirectJson jsonWriter, ILoggingEvent event) { |
69 |
| - jsonWriter.writeStringValue(LOGGER_ATTR_NAME, event.getLoggerName()); |
70 |
| - } |
71 |
| - |
72 |
| - public void writeThreadName(DirectJson jsonWriter, ILoggingEvent event) { |
73 |
| - jsonWriter.writeStringValue(THREAD_ATTR_NAME, event.getThreadName()); |
74 |
| - } |
75 |
| - |
76 |
| - public void writeLevel(DirectJson jsonWriter, ILoggingEvent event) { |
77 |
| - jsonWriter.writeStringValue(LEVEL_ATTR_NAME, event.getLevel().levelStr); |
78 |
| - } |
79 |
| - |
80 |
| - |
81 |
| - public void writeMarkers(DirectJson jsonWriter, ILoggingEvent event) { |
82 |
| - var markers = event.getMarkerList(); |
83 |
| - if (!markers.isEmpty()) { |
84 |
| - jsonWriter.openArray(MARKERS_ATTR_NAME); |
85 |
| - for (var marker : markers) { |
86 |
| - jsonWriter.writeString(marker.getName()); |
87 |
| - jsonWriter.writeSep(); |
88 |
| - } |
89 |
| - // Close array will overwrite the last "," in the buffer, so we are OK |
90 |
| - jsonWriter.closeArray(); |
91 |
| - jsonWriter.writeSep(); |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - public void writeMdc(DirectJson jsonWriter, ILoggingEvent event) { |
96 |
| - var mdc = event.getMDCPropertyMap(); |
97 |
| - if (!mdc.isEmpty()) { |
98 |
| - jsonWriter.openObject(MDC_ATTR_NAME); |
99 |
| - for (var entry : mdc.entrySet()) { |
100 |
| - jsonWriter.writeStringValue(entry.getKey(), entry.getValue()); |
101 |
| - } |
102 |
| - jsonWriter.closeObject(); |
103 |
| - jsonWriter.writeSep(); |
104 |
| - } |
105 |
| - } |
106 |
| - |
107 |
| - private void buildEmitterList() { |
108 |
| - // This method should be re-entrant and allow for reconfiguring the emitters if something change; |
109 |
| - emitters.clear(); |
110 |
| - |
111 |
| - // TODO figure out order |
112 |
| - if (includeLevel) emitters.add(this::writeLevel); |
113 |
| - if (includeMDC) emitters.add(this::writeMdc); |
114 |
| - if (includeMessage) emitters.add(this::writeMessage); |
115 |
| - if (includeFormattedMessage) emitters.add(this::writeFormattedMessage); |
116 |
| - if (includeThreadName) emitters.add(this::writeThreadName); |
117 |
| - if (includeLoggerName) emitters.add(this::writeLogger); |
118 |
| - // TODO add fields missing: |
119 |
| - // context |
120 |
| - // exception |
121 |
| - // custom data |
122 |
| - // marker |
123 |
| - } |
124 |
| - |
125 |
| - @Override |
126 |
| - public byte[] encode(ILoggingEvent event) { |
127 |
| - if (emitters.isEmpty()) { |
128 |
| - buildEmitterList(); |
129 |
| - } |
130 |
| - DirectJson jsonWriter = new DirectJson(); |
131 |
| - jsonWriter.openObject(); |
132 |
| - |
133 |
| - for (var emitter: emitters) { |
134 |
| - emitter.write(jsonWriter, event); |
135 |
| - } |
136 |
| - |
137 |
| - jsonWriter.closeObject(); |
138 |
| - return jsonWriter.flush(); |
139 |
| - } |
| 1 | +package ch.qos.logback.classic.encoder;public class JsonEncoder { |
140 | 2 | }
|
0 commit comments