Skip to content

Commit 6af1756

Browse files
jvzViliusSvy
authored
Adjust GcpLayout JSON to latest format (#3586) (#3867)
* 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. * revert severity changes, remove insertId * Remove insertid from tests * fix spotless error * Switch exception field to use exception resolver * try to fix timestamp tests * Fix tests with empty exceptions * Add changelog * Improve changelog. --------- Co-authored-by: Vilius Šumskas <[email protected]> Co-authored-by: Volkan Yazıcı <[email protected]>
1 parent 8de3a60 commit 6af1756

File tree

2 files changed

+23
-59
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

+23
-59
lines changed

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

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
import static org.apache.logging.log4j.layout.template.json.TestHelpers.usingSerializedLogEventAccessor;
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23-
import java.time.Instant;
24-
import java.time.ZoneId;
25-
import java.time.ZonedDateTime;
26-
import java.time.format.DateTimeFormatter;
27-
import java.util.Locale;
2823
import org.apache.logging.log4j.Level;
2924
import org.apache.logging.log4j.core.LogEvent;
3025
import org.apache.logging.log4j.core.impl.ContextDataFactory;
@@ -43,9 +38,6 @@ class GcpLayoutTest {
4338

4439
private static final int LOG_EVENT_COUNT = 1_000;
4540

46-
private static final DateTimeFormatter DATE_TIME_FORMATTER =
47-
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
48-
4941
@Test
5042
void test_lite_log_events() {
5143
LogEventFixture.createLiteLogEvents(LOG_EVENT_COUNT).forEach(GcpLayoutTest::verifySerialization);
@@ -83,8 +75,9 @@ private static void verifySerialization(final LogEvent logEvent) {
8375
usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> {
8476

8577
// Verify timestamp.
86-
final String expectedTimestamp = formatLogEventInstant(logEvent);
87-
assertThat(accessor.getString("timestamp")).isEqualTo(expectedTimestamp);
78+
final org.apache.logging.log4j.core.time.Instant instant = logEvent.getInstant();
79+
assertThat(accessor.getInteger("timestampSeconds")).isEqualTo(instant.getEpochSecond());
80+
assertThat(accessor.getInteger("timestampNanos")).isEqualTo(instant.getNanoOfSecond());
8881

8982
// Verify severity.
9083
final Level level = logEvent.getLevel();
@@ -147,48 +140,25 @@ private static void verifySerialization(final LogEvent logEvent) {
147140
.isEmpty();
148141
}
149142

150-
// Verify insert id.
151-
assertThat(accessor.getString("logging.googleapis.com/insertId")).matches("[-]?[0-9]+");
152-
153143
// Verify exception.
154144
if (exception != null) {
155145

156-
// Verify exception class.
157-
assertThat(accessor.getString(new String[] {"_exception", "class"}))
158-
.isEqualTo(exception.getClass().getCanonicalName());
159-
160-
// Verify exception message.
161-
assertThat(accessor.getString(new String[] {"_exception", "message"}))
162-
.isEqualTo(exception.getMessage());
163-
164146
// Verify exception stack trace.
165-
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
147+
assertThat(accessor.getString("exception"))
166148
.contains(exception.getLocalizedMessage())
167149
.contains("at org.apache.logging.log4j.layout.template.json")
168150
.contains("at java.base/java.lang.reflect.Method")
169151
.contains("at org.junit.platform.engine");
170152

171153
} else {
172-
assertThat(accessor.getObject(new String[] {"_exception", "class"}))
173-
.isNull();
174-
assertThat(accessor.getObject(new String[] {"_exception", "message"}))
175-
.isNull();
176-
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
177-
.isEmpty();
154+
assertThat(accessor.getString("exception")).isNull();
178155
}
179156

180157
// Verify thread name.
181-
assertThat(accessor.getString("_thread")).isEqualTo(logEvent.getThreadName());
158+
assertThat(accessor.getString("thread")).isEqualTo(logEvent.getThreadName());
182159

183160
// Verify logger name.
184-
assertThat(accessor.getString("_logger")).isEqualTo(logEvent.getLoggerName());
161+
assertThat(accessor.getString("logger")).isEqualTo(logEvent.getLoggerName());
185162
});
186163
}
187-
188-
private static String formatLogEventInstant(final LogEvent logEvent) {
189-
final org.apache.logging.log4j.core.time.Instant instant = logEvent.getInstant();
190-
final ZonedDateTime dateTime = Instant.ofEpochSecond(instant.getEpochSecond(), instant.getNanoOfSecond())
191-
.atZone(ZoneId.of("UTC"));
192-
return DATE_TIME_FORMATTER.format(dateTime);
193-
}
194164
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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
7+
}
8+
},
9+
"timestampNanos": {
10+
"$resolver": "timestamp",
11+
"epoch": {
12+
"unit": "secs.nanos"
813
}
914
},
1015
"severity": {
@@ -36,10 +41,6 @@
3641
"stackTraceEnabled": false
3742
}
3843
},
39-
"logging.googleapis.com/insertId": {
40-
"$resolver": "counter",
41-
"stringified": true
42-
},
4344
"logging.googleapis.com/trace": {
4445
"$resolver": "mdc",
4546
"key": "trace_id"
@@ -49,25 +50,18 @@
4950
"key": "span_id"
5051
},
5152
"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-
},
53+
"exception": {
54+
"$resolver": "exception",
55+
"field": "stackTrace",
6156
"stackTrace": {
62-
"$resolver": "pattern",
63-
"pattern": "%xEx"
57+
"stringified": true
6458
}
6559
},
66-
"_thread": {
60+
"thread": {
6761
"$resolver": "thread",
6862
"field": "name"
6963
},
70-
"_logger": {
64+
"logger": {
7165
"$resolver": "logger",
7266
"field": "name"
7367
}

0 commit comments

Comments
 (0)