Commit 3fb0a422 authored by Volkan Yazici's avatar Volkan Yazici
Browse files

LOG4J2-3116 Fix stack trace dumps.

parent 0448aa9b
Showing with 21 additions and 29 deletions
+21 -29
......@@ -14,7 +14,8 @@
},
"message": {
"$resolver": "pattern",
"pattern": "%m"
"pattern": "%m",
"stackTraceEnabled": true
},
"logging.googleapis.com/labels": {
"$resolver": "mdc",
......@@ -49,11 +50,8 @@
"field": "message"
},
"stackTrace": {
"$resolver": "exception",
"field": "stackTrace",
"stackTrace": {
"stringified": true
}
"$resolver": "pattern",
"pattern": "%xEx"
}
},
"_thread": {
......
......@@ -22,8 +22,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
......@@ -100,12 +98,18 @@ class GcpLayoutTest {
assertThat(accessor.getString("severity")).isEqualTo(expectedSeverity);
// Verify message.
final String expectedMessage = logEvent.getMessage().getFormattedMessage();
assertThat(accessor.getString("message")).contains(expectedMessage);
final String expectedExceptionStackTrace;
final Throwable exception = logEvent.getThrown();
if (exception != null) {
final String expectedExceptionMessage = exception.getLocalizedMessage();
assertThat(accessor.getString("message")).contains(expectedExceptionMessage);
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.util.stream.ForEachOps")
.contains("at org.junit.platform.engine");
} else {
expectedExceptionStackTrace = null;
}
// Verify labels.
......@@ -170,11 +174,12 @@ class GcpLayoutTest {
.isEqualTo(exception.getMessage());
// Verify exception stack trace.
final String expectedExceptionStackTrace =
serializeThrowableStackTrace(exception);
assertThat(accessor.getString(
new String[]{"_exception", "stackTrace"}))
.isEqualTo(expectedExceptionStackTrace);
.contains(exception.getLocalizedMessage())
.contains("at org.apache.logging.log4j.layout.template.json")
.contains("at java.util.stream.ForEachOps")
.contains("at org.junit.platform.engine");
} else {
assertThat(accessor.getObject(
......@@ -183,9 +188,9 @@ class GcpLayoutTest {
assertThat(accessor.getObject(
new String[]{"_exception", "message"}))
.isNull();
assertThat(accessor.getObject(
assertThat(accessor.getString(
new String[]{"_exception", "stackTrace"}))
.isNull();
.isEmpty();
}
// Verify thread name.
......@@ -207,15 +212,4 @@ class GcpLayoutTest {
return DATE_TIME_FORMATTER.format(dateTime);
}
private static String serializeThrowableStackTrace(final Throwable throwable) {
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final PrintWriter writer = new PrintWriter(outputStream)) {
throwable.printStackTrace(writer);
writer.flush();
return outputStream.toString(LAYOUT.getCharset().name());
} catch (final Exception error) {
throw new RuntimeException(error);
}
}
}
......@@ -31,7 +31,7 @@
-->
<release version="2.15.0" date="2021-MM-DD" description="GA Release 2.15.0">
<!-- ADDS -->
<action issue="LOG4J2-3116" dev="rgupta">
<action issue="LOG4J2-3116" dev="rgupta" type="add">
Add GCP logging layout.
</action>
<action issue="LOG4J2-3067" dev="vy" type="add">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment