-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
LOG4J2-3116 Google Cloud structured logging via JsonTemplate #543
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c507b9b
Google Cloud structured logging via JsonTemplate
rocketraman 64e3025
Minor code review updates
rocketraman 81aa8db
GCP layout test enhancement and stack trace fixes
rocketraman 14648a6
Merge branch 'release-2.x' into LOG4J2-3116
vy cc95baa
LOG4J2-3116 Fix changes.xml typo.
vy 3efc5da
LOG4J2-3116 Massage GcpLayout more.
vy 5ea3d85
LOG4J2-3116 Overhaul GcpLayoutTest.
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
log4j-layout-template-json/src/main/resources/GcpLayout.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"timestamp": { | ||
"$resolver": "timestamp", | ||
"pattern": { | ||
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | ||
"timeZone": "UTC", | ||
"locale": "en_US" | ||
} | ||
}, | ||
"severity": { | ||
"$resolver": "pattern", | ||
"pattern": "%level{WARN=WARNING, TRACE=DEBUG, FATAL=EMERGENCY}", | ||
"stackTraceEnabled": false | ||
}, | ||
"message": { | ||
"$resolver": "pattern", | ||
"pattern": "%m", | ||
"stackTraceEnabled": true | ||
}, | ||
"logging.googleapis.com/labels": { | ||
"$resolver": "mdc", | ||
"stringified": true | ||
}, | ||
"logging.googleapis.com/sourceLocation": { | ||
"file": { | ||
"$resolver": "source", | ||
"field": "fileName" | ||
}, | ||
"line": { | ||
"$resolver": "source", | ||
"field": "lineNumber" | ||
}, | ||
"function": { | ||
"$resolver": "pattern", | ||
"pattern": "%replace{%C.%M}{^\\?\\.$}{}", | ||
"stackTraceEnabled": false | ||
} | ||
}, | ||
"logging.googleapis.com/insertId": { | ||
"$resolver": "counter", | ||
"stringified": true | ||
}, | ||
"_exception": { | ||
"class": { | ||
"$resolver": "exception", | ||
"field": "className" | ||
}, | ||
"message": { | ||
"$resolver": "exception", | ||
"field": "message" | ||
}, | ||
"stackTrace": { | ||
"$resolver": "pattern", | ||
"pattern": "%xEx" | ||
} | ||
}, | ||
"_thread": { | ||
"$resolver": "thread", | ||
"field": "name" | ||
}, | ||
"_logger": { | ||
"$resolver": "logger", | ||
"field": "name" | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
...plate-json/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache license, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the license for the specific language governing permissions and | ||
* limitations under the license. | ||
*/ | ||
package org.apache.logging.log4j.layout.template.json; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
|
||
import static org.apache.logging.log4j.layout.template.json.TestHelpers.CONFIGURATION; | ||
import static org.apache.logging.log4j.layout.template.json.TestHelpers.usingSerializedLogEventAccessor; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class GcpLayoutTest { | ||
|
||
private static final JsonTemplateLayout LAYOUT = JsonTemplateLayout | ||
.newBuilder() | ||
.setConfiguration(CONFIGURATION) | ||
.setStackTraceEnabled(true) | ||
.setLocationInfoEnabled(true) | ||
.setEventTemplateUri("classpath:GcpLayout.json") | ||
.build(); | ||
|
||
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); | ||
} | ||
|
||
@Test | ||
void test_full_log_events() { | ||
LogEventFixture | ||
.createFullLogEvents(LOG_EVENT_COUNT) | ||
.forEach(GcpLayoutTest::verifySerialization); | ||
} | ||
|
||
private static void verifySerialization(final LogEvent logEvent) { | ||
usingSerializedLogEventAccessor(LAYOUT, logEvent, accessor -> { | ||
|
||
// Verify timestamp. | ||
final String expectedTimestamp = formatLogEventInstant(logEvent); | ||
assertThat(accessor.getString("timestamp")).isEqualTo(expectedTimestamp); | ||
|
||
// Verify severity. | ||
final Level level = logEvent.getLevel(); | ||
final String expectedSeverity; | ||
if (Level.WARN.equals(level)) { | ||
expectedSeverity = "WARNING"; | ||
} else if (Level.TRACE.equals(level)) { | ||
expectedSeverity = "TRACE"; | ||
} else if (Level.FATAL.equals(level)) { | ||
expectedSeverity = "EMERGENCY"; | ||
} else { | ||
expectedSeverity = level.name(); | ||
} | ||
assertThat(accessor.getString("severity")).isEqualTo(expectedSeverity); | ||
|
||
// Verify message. | ||
final Throwable exception = logEvent.getThrown(); | ||
if (exception != null) { | ||
final String actualMessage = accessor.getString("message"); | ||
assertThat(actualMessage) | ||
.contains(logEvent.getMessage().getFormattedMessage()) | ||
.contains(exception.getLocalizedMessage()) | ||
.contains("at org.apache.logging.log4j.layout.template.json") | ||
.contains("at java.lang.reflect.Method") | ||
.contains("at org.junit.platform.engine"); | ||
} | ||
|
||
// Verify labels. | ||
logEvent.getContextData().forEach((key, value) -> { | ||
final String expectedValue = String.valueOf(value); | ||
final String actualValue = | ||
accessor.getString(new String[]{ | ||
"logging.googleapis.com/labels", key}); | ||
assertThat(actualValue).isEqualTo(expectedValue); | ||
}); | ||
|
||
final StackTraceElement source = logEvent.getSource(); | ||
if (source != null) { | ||
|
||
// Verify file name. | ||
final String actualFileName = | ||
accessor.getString(new String[]{ | ||
"logging.googleapis.com/sourceLocation", "file"}); | ||
assertThat(actualFileName).isEqualTo(source.getFileName()); | ||
|
||
// Verify line number. | ||
final int actualLineNumber = | ||
accessor.getInteger(new String[]{ | ||
"logging.googleapis.com/sourceLocation", "line"}); | ||
assertThat(actualLineNumber).isEqualTo(source.getLineNumber()); | ||
|
||
// Verify function. | ||
final String expectedFunction = | ||
source.getClassName() + "." + source.getMethodName(); | ||
final String actualFunction = | ||
accessor.getString(new String[]{ | ||
"logging.googleapis.com/sourceLocation", "function"}); | ||
assertThat(actualFunction).isEqualTo(expectedFunction); | ||
|
||
} else { | ||
assertThat(accessor.exists( | ||
new String[]{"logging.googleapis.com/sourceLocation", "file"})) | ||
.isFalse(); | ||
assertThat(accessor.exists( | ||
new String[]{"logging.googleapis.com/sourceLocation", "line"})) | ||
.isFalse(); | ||
assertThat(accessor.getString( | ||
new String[]{"logging.googleapis.com/sourceLocation", "function"})) | ||
.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"})) | ||
.contains(exception.getLocalizedMessage()) | ||
.contains("at org.apache.logging.log4j.layout.template.json") | ||
.contains("at 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(); | ||
} | ||
|
||
// Verify thread name. | ||
assertThat(accessor.getString("_thread")) | ||
.isEqualTo(logEvent.getThreadName()); | ||
|
||
// Verify logger name. | ||
assertThat(accessor.getString("_logger")) | ||
.isEqualTo(logEvent.getLoggerName()); | ||
|
||
}); | ||
} | ||
|
||
private static String formatLogEventInstant(final LogEvent logEvent) { | ||
org.apache.logging.log4j.core.time.Instant instant = logEvent.getInstant(); | ||
ZonedDateTime dateTime = Instant.ofEpochSecond( | ||
instant.getEpochSecond(), | ||
instant.getNanoOfSecond()).atZone(ZoneId.of("UTC")); | ||
return DATE_TIME_FORMATTER.format(dateTime); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.