Skip to content

Commit

Permalink
- Logger nå causes rekursivt om ønskelig (default off, som før).
Browse files Browse the repository at this point in the history
#deploy-levende-arbeidsforhold-ansettelse
  • Loading branch information
rfc3092 committed Oct 24, 2024
1 parent 42c25f9 commit 41ebc63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<appender name="stdout_json" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="no.nav.testnav.libs.reactivecore.logging.TestnavLogbackEncoder">
<maxStackTraceLength>-1</maxStackTraceLength>
<addCauses>true</addCauses>
<!-- <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">-->
<!-- <rootCauseFirst>true</rootCauseFirst>-->
<!-- -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.testnav.libs.reactivecore.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxy;
import com.fasterxml.jackson.core.JsonFactory;
import lombok.Setter;
Expand All @@ -18,6 +19,7 @@
/**
* Config:
* <li>{@code maxStackTraceLength}: Default 480, set to a negative number to disable truncation of stack trace altogether.</li>
* <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>
*/
@Slf4j
public class TestnavLogbackEncoder extends LogstashEncoder {
Expand All @@ -28,6 +30,9 @@ public class TestnavLogbackEncoder extends LogstashEncoder {
@Setter
private int maxStackTraceLength = 480;

@Setter
private boolean addCauses = false;

@SneakyThrows
@Override
public byte[] encode(ILoggingEvent event) {
Expand All @@ -50,6 +55,9 @@ public byte[] encode(ILoggingEvent event) {
for (StackTraceElement element : exception.getThrowable().getStackTrace()) {
pw.println("\tat " + element);
}
if (addCauses) {
recursivelyAddCauses(exception, pw);
}
var stackTrace = maxStackTraceLength < 0 ? sw.toString() : sw.toString().substring(0, maxStackTraceLength);
generator.writeStringField("stack_trace", stackTrace);
}
Expand All @@ -63,6 +71,19 @@ public byte[] encode(ILoggingEvent event) {
return outputStream.toByteArray();
}

private static void recursivelyAddCauses(IThrowableProxy exception, PrintWriter pw) {
var cause = exception.getCause();
if (cause != null) {
pw
.append("caused by ")
.append(cause.getClassName())
.append(": ")
.append(cause.getMessage())
.append("\n");
recursivelyAddCauses(cause, pw);
}
}

private String formatMessage(String message) {
var matcher = pattern.matcher(message);

Expand Down

0 comments on commit 41ebc63

Please sign in to comment.