Skip to content
Merged
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 @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
Expand All @@ -14,6 +15,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

private <P extends ComposerBase> void addStacktrace(ObjectComposer<P> oc, ILoggingEvent event) throws IOException,
JsonProcessingException {
if (event.getThrowableProxy() != null) {
List<String> lines = Stream.of(event.getThrowableProxy().getStackTraceElementProxyArray()).map(
StackTraceElementProxy::getSTEAsString)
.collect(Collectors.toList());
IThrowableProxy proxy = event.getThrowableProxy();
if (proxy != null && ThrowableProxy.class.isAssignableFrom(proxy.getClass())) {
Throwable throwable = ((ThrowableProxy) proxy).getThrowable();
LineWriter lw = new LineWriter();
throwable.printStackTrace(new PrintWriter(lw));
List<String> lines = lw.getLines();
StacktraceLines stacktraceLines = new StacktraceLines(lines);
ArrayComposer<ObjectComposer<P>> ac = oc.startArrayField(Fields.STACKTRACE);
if (stacktraceLines.getTotalLineLength() <= maxStacktraceSize) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.sap.hcp.cf.logging.common;

import static com.sap.hcp.cf.logging.common.converter.CustomFieldMatchers.hasCustomField;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;

Expand Down Expand Up @@ -122,20 +121,17 @@ public void testCustomFieldOverwritesMdc() throws Exception {
assertThat(getField(SOME_KEY), is(SOME_OTHER_VALUE));
}

@Test
@Test
public void testStacktrace() {
try {
Double.parseDouble(null);
} catch (Exception ex) {
logMsg = "Running testStacktrace()";
LOGGER.error(logMsg, ex);
assertThat(getMessage(), is(logMsg));
assertThat(getField(Fields.COMPONENT_ID), is("-"));
assertThat(getField(Fields.COMPONENT_NAME), is("-"));
assertThat(getField(Fields.COMPONENT_INSTANCE), is("0"));
assertThat(getField(Fields.STACKTRACE), is(notNullValue()));
assertThat(getField(Fields.WRITTEN_TS), is(notNullValue()));
}
logMsg = "Running testStacktrace()";
Exception testEx = new Exception("Test-case for Stacktraces.");
LOGGER.error(logMsg, testEx);
assertThat(getMessage(), is(logMsg));
assertThat(getField(Fields.COMPONENT_ID), is("-"));
assertThat(getField(Fields.COMPONENT_NAME), is("-"));
assertThat(getField(Fields.COMPONENT_INSTANCE), is("0"));
assertThat(getField(Fields.STACKTRACE), stringContainsInOrder(asList("java.lang.Exception", "Test-case for Stacktraces.")));
assertThat(getField(Fields.WRITTEN_TS), is(notNullValue()));
}

@Test
Expand Down