Skip to content

Adjust GcpLayout JSON to latest format #3586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
import static org.apache.logging.log4j.layout.template.json.TestHelpers.usingSerializedLogEventAccessor;
import static org.assertj.core.api.Assertions.assertThat;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ContextDataFactory;
Expand All @@ -44,9 +39,6 @@ class GcpLayoutTest {

private static final int LOG_EVENT_COUNT = 1_000;

private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);

@Test
void test_lite_log_events() {
LogEventFixture.createLiteLogEvents(LOG_EVENT_COUNT).forEach(GcpLayoutTest::verifySerialization);
Expand Down Expand Up @@ -84,8 +76,9 @@ private static void verifySerialization(final LogEvent logEvent) {
usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> {

// Verify timestamp.
final String expectedTimestamp = formatLogEventInstant(logEvent);
assertThat(accessor.getString("timestamp")).isEqualTo(expectedTimestamp);
final org.apache.logging.log4j.core.time.Instant instant = logEvent.getInstant();
assertThat(accessor.getInteger("timestampSeconds")).isEqualTo(instant.getEpochSecond());
assertThat(accessor.getInteger("timestampNanos")).isEqualTo(instant.getNanoOfSecond());

// Verify severity.
final Level level = logEvent.getLevel();
Expand Down Expand Up @@ -148,48 +141,25 @@ private static void verifySerialization(final LogEvent logEvent) {
.isEmpty();
}

// Verify insert id.
assertThat(accessor.getString("logging.googleapis.com/insertId")).matches("[-]?[0-9]+");

// Verify exception.
if (exception != null) {

// Verify exception class.
assertThat(accessor.getString(new String[] {"_exception", "class"}))
.isEqualTo(exception.getClass().getCanonicalName());

// Verify exception message.
assertThat(accessor.getString(new String[] {"_exception", "message"}))
.isEqualTo(exception.getMessage());

// Verify exception stack trace.
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
assertThat(accessor.getString("exception"))
.contains(exception.getLocalizedMessage())
.contains("at org.apache.logging.log4j.layout.template.json")
.contains("at " + JAVA_BASE_PREFIX + "java.lang.reflect.Method")
.contains("at org.junit.platform.engine");

} else {
assertThat(accessor.getObject(new String[] {"_exception", "class"}))
.isNull();
assertThat(accessor.getObject(new String[] {"_exception", "message"}))
.isNull();
assertThat(accessor.getString(new String[] {"_exception", "stackTrace"}))
.isEmpty();
assertThat(accessor.getString("exception")).isNull();
}

// Verify thread name.
assertThat(accessor.getString("_thread")).isEqualTo(logEvent.getThreadName());
assertThat(accessor.getString("thread")).isEqualTo(logEvent.getThreadName());

// Verify logger name.
assertThat(accessor.getString("_logger")).isEqualTo(logEvent.getLoggerName());
assertThat(accessor.getString("logger")).isEqualTo(logEvent.getLoggerName());
});
}

private static String formatLogEventInstant(final LogEvent logEvent) {
final org.apache.logging.log4j.core.time.Instant instant = logEvent.getInstant();
final ZonedDateTime dateTime = Instant.ofEpochSecond(instant.getEpochSecond(), instant.getNanoOfSecond())
.atZone(ZoneId.of("UTC"));
return DATE_TIME_FORMATTER.format(dateTime);
}
}
38 changes: 16 additions & 22 deletions log4j-layout-template-json/src/main/resources/GcpLayout.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"timestamp": {
"timestampSeconds": {
"$resolver": "timestamp",
"pattern": {
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"timeZone": "UTC",
"locale": "en_US"
"epoch": {
"unit": "secs",
"rounded": true
}
},
"timestampNanos": {
"$resolver": "timestamp",
"epoch": {
"unit": "secs.nanos"
}
},
"severity": {
Expand Down Expand Up @@ -36,10 +41,6 @@
"stackTraceEnabled": false
}
},
"logging.googleapis.com/insertId": {
"$resolver": "counter",
"stringified": true
},
"logging.googleapis.com/trace": {
"$resolver": "mdc",
"key": "trace_id"
Expand All @@ -49,25 +50,18 @@
"key": "span_id"
},
"logging.googleapis.com/trace_sampled": true,
"_exception": {
"class": {
"$resolver": "exception",
"field": "className"
},
"message": {
"$resolver": "exception",
"field": "message"
},
"exception": {
"$resolver": "exception",
"field": "stackTrace",
"stackTrace": {
"$resolver": "pattern",
"pattern": "%xEx"
"stringified": true
}
},
"_thread": {
"thread": {
"$resolver": "thread",
"field": "name"
},
"_logger": {
"logger": {
"$resolver": "logger",
"field": "name"
}
Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/3586_improve_GcpLayout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="3586" link="https://github.com/apache/logging-log4j2/pull/3586"/>
<description format="asciidoc">
Adjust GcpLayout JSON template to support automatic timestamp recognition by the Google Cloud Logging. This also changes `exception`, `thread`, `logger` fields a bit, and removes `insertId` field.
</description>
</entry>