Skip to content

Commit 10d5703

Browse files
Fix logback stacktrace information
Since v3.6.0 the stacktrace feature was not working properly in the logback implementation. Amongst the missing information was the exception class, the exception message and suppressed or caused exceptions. The proposed change brings back the original coding used before v3.6.0, which is also used in the log4j2 implementation. Signed-off-by: Karsten Schnitter <[email protected]>
1 parent 4564921 commit 10d5703

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

cf-java-logging-support-logback/src/main/java/com/sap/hcp/cf/logback/encoder/JsonEncoder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.OutputStream;
5+
import java.io.PrintWriter;
56
import java.io.StringWriter;
67
import java.lang.reflect.InvocationTargetException;
78
import java.nio.charset.Charset;
@@ -14,6 +15,9 @@
1415
import java.util.stream.Collectors;
1516
import java.util.stream.Stream;
1617

18+
import ch.qos.logback.classic.spi.IThrowableProxy;
19+
import ch.qos.logback.classic.spi.ThrowableProxy;
20+
import com.sap.hcp.cf.logging.common.converter.LineWriter;
1721
import org.slf4j.Logger;
1822
import org.slf4j.LoggerFactory;
1923
import org.slf4j.Marker;
@@ -330,10 +334,12 @@ private Map<String, Object> collectContextFields(ILoggingEvent event) {
330334

331335
private <P extends ComposerBase> void addStacktrace(ObjectComposer<P> oc, ILoggingEvent event) throws IOException,
332336
JsonProcessingException {
333-
if (event.getThrowableProxy() != null) {
334-
List<String> lines = Stream.of(event.getThrowableProxy().getStackTraceElementProxyArray()).map(
335-
StackTraceElementProxy::getSTEAsString)
336-
.collect(Collectors.toList());
337+
IThrowableProxy proxy = event.getThrowableProxy();
338+
if (proxy != null && ThrowableProxy.class.isAssignableFrom(proxy.getClass())) {
339+
Throwable throwable = ((ThrowableProxy) proxy).getThrowable();
340+
LineWriter lw = new LineWriter();
341+
throwable.printStackTrace(new PrintWriter(lw));
342+
List<String> lines = lw.getLines();
337343
StacktraceLines stacktraceLines = new StacktraceLines(lines);
338344
ArrayComposer<ObjectComposer<P>> ac = oc.startArrayField(Fields.STACKTRACE);
339345
if (stacktraceLines.getTotalLineLength() <= maxStacktraceSize) {

cf-java-logging-support-logback/src/test/java/com/sap/hcp/cf/logging/common/TestAppLog.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.sap.hcp.cf.logging.common;
22

33
import static com.sap.hcp.cf.logging.common.converter.CustomFieldMatchers.hasCustomField;
4+
import static java.util.Arrays.asList;
45
import static org.hamcrest.MatcherAssert.assertThat;
5-
import static org.hamcrest.Matchers.contains;
6-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
7-
import static org.hamcrest.Matchers.lessThanOrEqualTo;
6+
import static org.hamcrest.Matchers.*;
87
import static org.hamcrest.core.Is.is;
98
import static org.hamcrest.core.IsNull.notNullValue;
109

@@ -122,20 +121,17 @@ public void testCustomFieldOverwritesMdc() throws Exception {
122121
assertThat(getField(SOME_KEY), is(SOME_OTHER_VALUE));
123122
}
124123

125-
@Test
124+
@Test
126125
public void testStacktrace() {
127-
try {
128-
Double.parseDouble(null);
129-
} catch (Exception ex) {
130-
logMsg = "Running testStacktrace()";
131-
LOGGER.error(logMsg, ex);
132-
assertThat(getMessage(), is(logMsg));
133-
assertThat(getField(Fields.COMPONENT_ID), is("-"));
134-
assertThat(getField(Fields.COMPONENT_NAME), is("-"));
135-
assertThat(getField(Fields.COMPONENT_INSTANCE), is("0"));
136-
assertThat(getField(Fields.STACKTRACE), is(notNullValue()));
137-
assertThat(getField(Fields.WRITTEN_TS), is(notNullValue()));
138-
}
126+
logMsg = "Running testStacktrace()";
127+
Exception testEx = new Exception("Test-case for Stacktraces.");
128+
LOGGER.error(logMsg, testEx);
129+
assertThat(getMessage(), is(logMsg));
130+
assertThat(getField(Fields.COMPONENT_ID), is("-"));
131+
assertThat(getField(Fields.COMPONENT_NAME), is("-"));
132+
assertThat(getField(Fields.COMPONENT_INSTANCE), is("0"));
133+
assertThat(getField(Fields.STACKTRACE), stringContainsInOrder(asList("java.lang.Exception", "Test-case for Stacktraces.")));
134+
assertThat(getField(Fields.WRITTEN_TS), is(notNullValue()));
139135
}
140136

141137
@Test

0 commit comments

Comments
 (0)