- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJSONLayout.java
More file actions
Latest commit
174 lines (149 loc) · 4.92 KB
/
Copy pathJSONLayout.java
File metadata and controls
174 lines (149 loc) · 4.92 KB
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
/**
* Default package
*/
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.io.StringWriter;
importjava.nio.charset.Charset;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importjava.util.TreeSet;
importorg.apache.commons.lang.StringUtils;
importorg.apache.logging.log4j.ThreadContext.ContextStack;
importorg.apache.logging.log4j.core.LogEvent;
importorg.apache.logging.log4j.core.config.plugins.Plugin;
importorg.apache.logging.log4j.core.config.plugins.PluginAttr;
importorg.apache.logging.log4j.core.config.plugins.PluginFactory;
importorg.apache.logging.log4j.core.layout.AbstractStringLayout;
importorg.apache.logging.log4j.message.MapMessage;
importorg.apache.logging.log4j.message.Message;
importorg.codehaus.jackson.JsonFactory;
importorg.codehaus.jackson.JsonGenerator;
/**
* Log4j 2.x layout for logging events as JSON.
*
* Based on
* <a href="https://github.com/Aconex/json-log4j-layout">Aconex/json-log4j-layout</a>
* <br/>
* which also inspired <a href="https://github.com/carrot-garden/carrot-log">carrot-garden/carrot-log</a>
*
*
* @author bowering
*/
@Plugin(name = "JSONLayout", type = "Core", elementType = "layout", printObject = true)
publicclassJSONLayoutextendsAbstractStringLayout {
publicstaticfinalCharsetUTF8 = Charset.forName("UTF-8");
privatefinalJsonFactoryjsonFactory = newJsonFactory();
/**
* Constructor
*
* @param charset
*/
protectedJSONLayout(finalCharsetcharset) {
super(charset);
}
/*
* @see
* org.apache.logging.log4j.core.Layout#toSerializable(org.apache.logging.
* log4j.core.LogEvent)
*/
@Override
publicStringtoSerializable(finalLogEventevent) {
try {
finalStringWriterstringWriter = newStringWriter();
finalJsonGeneratorg = jsonFactory.createJsonGenerator(stringWriter);
g.writeStartObject();
writeBasicFields(event, g);
writeMessageField(event, g);
writeMDC(event, g);
writeNDC(event, g);
writeThrowableEvents(event, g);
g.writeEndObject();
g.close();
stringWriter.append("\n");
returnstringWriter.toString();
} catch (IOExceptione) {
LOGGER.error("Could not write event as JSON", e);
}
returnStringUtils.EMPTY;
}
privatestaticvoidwriteBasicFields(finalLogEventevent, finalJsonGeneratorg)
throwsIOException {
g.writeStringField("logger", event.getLoggerName());
g.writeStringField("level", event.getLevel().toString());
g.writeNumberField("timestamp", event.getMillis());
g.writeStringField("threadName", event.getThreadName());
}
privatestaticvoidwriteMessageField(finalLogEventevent,
finalJsonGeneratorg) throwsIOException {
finalMessagemessage = event.getMessage();
if (message == null) return;
if (messageinstanceofMapMessage) {
finalMapMessagemapMessage = (MapMessage) message;
finalMap<String, String> map = mapMessage.getData();
writeStringMap("msg", map, g);
} else {
g.writeStringField("msg", message.toString());
}
}
privatestaticvoidwriteMDC(finalLogEventevent, finalJsonGeneratorg) throwsIOException {
writeStringMap("mdc", event.getContextMap(), g);
}
privatestaticvoidwriteNDC(finalLogEventevent, finalJsonGeneratorg) throwsIOException {
finalContextStackndc = event.getContextStack();
if (ndc == null || ndc.getDepth() == 0) return;
finalList<String> ndcList = ndc.asList();
g.writeArrayFieldStart("ndc");
for (finalStringstackElement : ndcList) {
g.writeString(stackElement);
}
g.writeEndArray();
}
/**
* Write the given stringMap as a JSON object with the given mapName
*
* @param mapName
* @param stringMap
* @param g
* @throws IOException
*/
privatestaticvoidwriteStringMap(finalStringmapName, finalMap<String, String> stringMap, finalJsonGeneratorg) throwsIOException {
if (stringMap == null || stringMap.isEmpty()) return;
g.writeFieldName(mapName);
g.writeStartObject();
// TreeSet orders fields alphabetically by key:
finalSet<String> keys = newTreeSet<String>(stringMap.keySet());
for (finalStringkey : keys) {
g.writeStringField(key, stringMap.get(key));
}
g.writeEndObject();
}
privatestaticvoidwriteThrowableEvents(finalLogEventevent,
finalJsonGeneratorg) throwsIOException {
finalThrowablethrown = event.getThrown();
if (thrown == null)
return;
StringWritersw = newStringWriter();
PrintWriterpw = newPrintWriter(sw);
thrown.printStackTrace(pw);
StringthrowableString = sw.toString();
if (throwableString.isEmpty())
return;
g.writeStringField("throwable", throwableString);
}
@PluginFactory
publicstaticJSONLayoutcreateLayout(
@PluginAttr("charset") finalStringcharset) {
Charsetc = UTF8;
if (charset != null) {
if (Charset.isSupported(charset)) {
c = Charset.forName(charset);
} else {
LOGGER.error("Charset " + charset
+ " is not supported for layout, using " + c.displayName());
}
}
returnnewJSONLayout(c);
}
}