Skip to content

Commit 1acbf69

Browse files
committed
Logger kun no.nav.testnav i stack trace.
#deploy-levende-arbeidsforhold-ansettelse
1 parent 41ebc63 commit 1acbf69

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml

+1-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,7 @@
55
<encoder class="no.nav.testnav.libs.reactivecore.logging.TestnavLogbackEncoder">
66
<maxStackTraceLength>-1</maxStackTraceLength>
77
<addCauses>true</addCauses>
8-
<!-- <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">-->
9-
<!-- <rootCauseFirst>true</rootCauseFirst>-->
10-
<!-- -->
11-
<!-- <maxLength>10280</maxLength>-->
12-
<!-- <shortenedClassNameLength>20</shortenedClassNameLength>-->
13-
<!-- <exclude>^sun\.reflect\..*\.invoke</exclude>-->
14-
<!-- <exclude>^net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>-->
15-
<!-- <exclude>java\.util\.concurrent\..*</exclude>-->
16-
<!-- <exclude>org\.apache\.catalina\..*</exclude>-->
17-
<!-- <exclude>org\.apache\.coyote\..*</exclude>-->
18-
<!-- <exclude>org\.apache\.tomcat\..*</exclude>-->
19-
<!-- </throwableConverter>-->
8+
<stackTraceIncludePrefix>no.nav.testnav</stackTraceIncludePrefix>
209
</encoder>
2110
</appender>
2211
<root level="INFO">

libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/logging/TestnavLogbackEncoder.java

+57-29
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
package no.nav.testnav.libs.reactivecore.logging;
22

33
import ch.qos.logback.classic.spi.ILoggingEvent;
4-
import ch.qos.logback.classic.spi.IThrowableProxy;
54
import ch.qos.logback.classic.spi.ThrowableProxy;
65
import com.fasterxml.jackson.core.JsonFactory;
76
import lombok.Setter;
87
import lombok.SneakyThrows;
98
import lombok.extern.slf4j.Slf4j;
109
import net.logstash.logback.encoder.LogstashEncoder;
10+
import org.springframework.util.StringUtils;
1111

1212
import java.io.ByteArrayOutputStream;
13-
import java.io.PrintWriter;
1413
import java.io.StringWriter;
1514
import java.util.regex.Pattern;
1615

16+
import static java.util.Objects.isNull;
1717
import static java.util.Objects.nonNull;
18+
import static org.springframework.util.StringUtils.truncate;
1819

1920
/**
2021
* Config:
21-
* <li>{@code maxStackTraceLength}: Default 480, set to a negative number to disable truncation of stack trace altogether.</li>
22-
* <li>@{code addCauses}: If {@code true}, adds the cause(s) to the stack trace (without stack traces for each cause) Truncated according to above.</li>
22+
* <ul>
23+
* <li>{@code maxStackTraceLength}: Default 480, set to <1 to disable truncation of stack trace altogether.</li>
24+
* <li>{@code addCauses}: If enabled, adds the cause(s) to the stack trace (with stack traces for each cause) Truncated according to above, filtered according to below.</li>
25+
* <li>{@code stackTraceIncludePrefix}: If set, only include stack trace elements that starts with the given prefix, e.g. "no.nav.testnav".</li>
26+
* </ul>
27+
* Copy of {@code no.nav.testnav.libs.servletcore.logging.TestnavLogbackEncoder}.
28+
*
29+
* @see StringUtils#truncate(CharSequence, int)
2330
*/
2431
@Slf4j
32+
@SuppressWarnings("java:S110")
2533
public class TestnavLogbackEncoder extends LogstashEncoder {
2634

2735
// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
@@ -33,6 +41,9 @@ public class TestnavLogbackEncoder extends LogstashEncoder {
3341
@Setter
3442
private boolean addCauses = false;
3543

44+
@Setter
45+
private String stackTraceIncludePrefix = null;
46+
3647
@SneakyThrows
3748
@Override
3849
public byte[] encode(ILoggingEvent event) {
@@ -47,21 +58,7 @@ public byte[] encode(ILoggingEvent event) {
4758
generator.writeStringField("logger_name", event.getLoggerName());
4859
generator.writeStringField("thread_name", event.getThreadName());
4960
generator.writeStringField("level", event.getLevel().toString());
50-
if (nonNull(event.getThrowableProxy())) {
51-
var exception = (ThrowableProxy) event.getThrowableProxy();
52-
if (nonNull(exception.getThrowable())) {
53-
var sw = new StringWriter();
54-
var pw = new PrintWriter(sw);
55-
for (StackTraceElement element : exception.getThrowable().getStackTrace()) {
56-
pw.println("\tat " + element);
57-
}
58-
if (addCauses) {
59-
recursivelyAddCauses(exception, pw);
60-
}
61-
var stackTrace = maxStackTraceLength < 0 ? sw.toString() : sw.toString().substring(0, maxStackTraceLength);
62-
generator.writeStringField("stack_trace", stackTrace);
63-
}
64-
}
61+
generator.writeStringField("stack_trace", getStackTrace(event));
6562
generator.writeEndObject();
6663

6764
generator.flush();
@@ -71,16 +68,47 @@ public byte[] encode(ILoggingEvent event) {
7168
return outputStream.toByteArray();
7269
}
7370

74-
private static void recursivelyAddCauses(IThrowableProxy exception, PrintWriter pw) {
75-
var cause = exception.getCause();
76-
if (cause != null) {
77-
pw
78-
.append("caused by ")
79-
.append(cause.getClassName())
80-
.append(": ")
81-
.append(cause.getMessage())
82-
.append("\n");
83-
recursivelyAddCauses(cause, pw);
71+
private String getStackTrace(ILoggingEvent event) {
72+
if (nonNull(event.getThrowableProxy())) {
73+
var exception = (ThrowableProxy) event.getThrowableProxy();
74+
if (nonNull(exception.getThrowable())) {
75+
var writer = new StringWriter();
76+
appendStackTraceElements(exception.getThrowable().getStackTrace(), writer);
77+
appendStackTraceCauses(exception, writer);
78+
return maxStackTraceLength > 0 ?
79+
truncate(writer.toString(), maxStackTraceLength) :
80+
writer.toString();
81+
}
82+
}
83+
return null;
84+
}
85+
86+
private void appendStackTraceElements(StackTraceElement[] elements, StringWriter writer) {
87+
for (StackTraceElement element : elements) {
88+
if (isNull(stackTraceIncludePrefix) || stackTraceIncludePrefix.isEmpty() || element.toString().startsWith(stackTraceIncludePrefix)) {
89+
writer
90+
.append("\tat ")
91+
.append(element.toString())
92+
.append("\n");
93+
}
94+
}
95+
}
96+
97+
private void appendStackTraceCauses(ThrowableProxy exception, StringWriter writer) {
98+
if (addCauses) {
99+
var cause = exception;
100+
while (!isNull(cause.getCause())) {
101+
cause = (ThrowableProxy) cause.getCause();
102+
writer
103+
.append("caused by ")
104+
.append(cause.getClassName())
105+
.append(": ")
106+
.append(cause.getMessage())
107+
.append("\n");
108+
if (!isNull(cause.getThrowable())) {
109+
appendStackTraceElements(cause.getThrowable().getStackTrace(), writer);
110+
}
111+
}
84112
}
85113
}
86114

0 commit comments

Comments
 (0)