Skip to content

Commit 8910fcb

Browse files
committed
Adjust GcpLayout JSON to latest format
First, it formats the log timestamp field to the correct format recognized by Fluent-Bit (component of Google Cloud Logging) and Google Ops Agent. Secondly, severity field now must be prefixed with logging.googleapis.com. Third, counter cannot be used for insertId as it is duplicated on different threads. And the last but not the least, exception, thread and logger fields are pretty standard when logging via Logback's JSON layout and Google's Spring GCP libraries. Field name changes now match these other loggers.
1 parent 4d598e0 commit 8910fcb

File tree

2 files changed

+24
-40
lines changed
  • log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json
  • log4j-layout-template-json/src/main/resources

2 files changed

+24
-40
lines changed

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,40 +149,28 @@ private static void verifySerialization(final LogEvent logEvent) {
149149
}
150150

151151
// Verify insert id.
152-
assertThat(accessor.getString("logging.googleapis.com/insertId")).matches("[-]?[0-9]+");
152+
assertThat(accessor.getString("logging.googleapis.com/insertId")).matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
153153

154154
// Verify exception.
155155
if (exception != null) {
156156

157-
// Verify exception class.
158-
assertThat(accessor.getString(new String[] {"_exception", "class"}))
159-
.isEqualTo(exception.getClass().getCanonicalName());
160-
161-
// Verify exception message.
162-
assertThat(accessor.getString(new String[] {"_exception", "message"}))
163-
.isEqualTo(exception.getMessage());
164-
165157
// Verify exception stack trace.
166-
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
158+
assertThat(accessor.getString("exception"))
167159
.contains(exception.getLocalizedMessage())
168160
.contains("at org.apache.logging.log4j.layout.template.json")
169161
.contains("at " + JAVA_BASE_PREFIX + "java.lang.reflect.Method")
170162
.contains("at org.junit.platform.engine");
171163

172164
} else {
173-
assertThat(accessor.getObject(new String[] {"_exception", "class"}))
174-
.isNull();
175-
assertThat(accessor.getObject(new String[] {"_exception", "message"}))
176-
.isNull();
177-
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
165+
assertThat(accessor.getString("exception"))
178166
.isEmpty();
179167
}
180168

181169
// Verify thread name.
182-
assertThat(accessor.getString("_thread")).isEqualTo(logEvent.getThreadName());
170+
assertThat(accessor.getString("thread")).isEqualTo(logEvent.getThreadName());
183171

184172
// Verify logger name.
185-
assertThat(accessor.getString("_logger")).isEqualTo(logEvent.getLoggerName());
173+
assertThat(accessor.getString("logger")).isEqualTo(logEvent.getLoggerName());
186174
});
187175
}
188176

log4j-layout-template-json/src/main/resources/GcpLayout.json

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
2-
"timestamp": {
2+
"timestampSeconds": {
33
"$resolver": "timestamp",
4-
"pattern": {
5-
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
6-
"timeZone": "UTC",
7-
"locale": "en_US"
4+
"epoch": {
5+
"unit": "secs",
6+
"rounded": true
87
}
98
},
10-
"severity": {
9+
"timestampNanos": {
10+
"$resolver": "timestamp",
11+
"epoch": {
12+
"unit": "secs.nanos"
13+
}
14+
},
15+
"logging.googleapis.com/severity": {
1116
"$resolver": "pattern",
1217
"pattern": "%level{WARN=WARNING, TRACE=DEBUG, FATAL=EMERGENCY}",
1318
"stackTraceEnabled": false
@@ -37,8 +42,9 @@
3742
}
3843
},
3944
"logging.googleapis.com/insertId": {
40-
"$resolver": "counter",
41-
"stringified": true
45+
"$resolver": "pattern",
46+
"pattern": "%uuid{TIME}",
47+
"stackTraceEnabled": false
4248
},
4349
"logging.googleapis.com/trace": {
4450
"$resolver": "mdc",
@@ -49,25 +55,15 @@
4955
"key": "span_id"
5056
},
5157
"logging.googleapis.com/trace_sampled": true,
52-
"_exception": {
53-
"class": {
54-
"$resolver": "exception",
55-
"field": "className"
56-
},
57-
"message": {
58-
"$resolver": "exception",
59-
"field": "message"
60-
},
61-
"stackTrace": {
62-
"$resolver": "pattern",
63-
"pattern": "%xEx"
64-
}
58+
"exception": {
59+
"$resolver": "pattern",
60+
"pattern": "%xEx"
6561
},
66-
"_thread": {
62+
"thread": {
6763
"$resolver": "thread",
6864
"field": "name"
6965
},
70-
"_logger": {
66+
"logger": {
7167
"$resolver": "logger",
7268
"field": "name"
7369
}

0 commit comments

Comments
 (0)