From 41ebc635e70b614f5072914ad9d1c5f304b1469b Mon Sep 17 00:00:00 2001 From: Cato Olsen Date: Thu, 24 Oct 2024 14:57:05 +0200 Subject: [PATCH] =?UTF-8?q?-=20Logger=20n=C3=A5=20causes=20rekursivt=20om?= =?UTF-8?q?=20=C3=B8nskelig=20(default=20off,=20som=20f=C3=B8r).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #deploy-levende-arbeidsforhold-ansettelse --- .../src/main/resources/logback-spring.xml | 1 + .../logging/TestnavLogbackEncoder.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml b/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml index e21ea8ac38f..72ecf8f1337 100644 --- a/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml +++ b/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml @@ -4,6 +4,7 @@ -1 + true diff --git a/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/logging/TestnavLogbackEncoder.java b/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/logging/TestnavLogbackEncoder.java index add94f247f1..bdcfb6cacd2 100644 --- a/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/logging/TestnavLogbackEncoder.java +++ b/libs/reactive-core/src/main/java/no/nav/testnav/libs/reactivecore/logging/TestnavLogbackEncoder.java @@ -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; @@ -18,6 +19,7 @@ /** * Config: *
  • {@code maxStackTraceLength}: Default 480, set to a negative number to disable truncation of stack trace altogether.
  • + *
  • @{code addCauses}: If {@code true}, adds the cause(s) to the stack trace (without stack traces for each cause) Truncated according to above.
  • */ @Slf4j public class TestnavLogbackEncoder extends LogstashEncoder { @@ -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) { @@ -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); } @@ -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);