From 1acbf691af724491c5d83c80e25c637065755cff Mon Sep 17 00:00:00 2001 From: Cato Olsen Date: Fri, 25 Oct 2024 11:19:43 +0200 Subject: [PATCH] Logger kun no.nav.testnav i stack trace. #deploy-levende-arbeidsforhold-ansettelse --- .../src/main/resources/logback-spring.xml | 13 +-- .../logging/TestnavLogbackEncoder.java | 86 ++++++++++++------- 2 files changed, 58 insertions(+), 41 deletions(-) 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 72ecf8f133..4f7136b7de 100644 --- a/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml +++ b/apps/levende-arbeidsforhold-ansettelse/src/main/resources/logback-spring.xml @@ -5,18 +5,7 @@ -1 true - - - - - - - - - - - - + no.nav.testnav 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 bdcfb6cacd..67acbbe5fe 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,27 +1,35 @@ 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; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import net.logstash.logback.encoder.LogstashEncoder; +import org.springframework.util.StringUtils; import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; import java.io.StringWriter; import java.util.regex.Pattern; +import static java.util.Objects.isNull; import static java.util.Objects.nonNull; +import static org.springframework.util.StringUtils.truncate; /** * 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.
  • + * + * Copy of {@code no.nav.testnav.libs.servletcore.logging.TestnavLogbackEncoder}. + * + * @see StringUtils#truncate(CharSequence, int) */ @Slf4j +@SuppressWarnings("java:S110") public class TestnavLogbackEncoder extends LogstashEncoder { // matches exactly 11 digits (\\d{11}) that are not immediately preceded ((? 0 ? + truncate(writer.toString(), maxStackTraceLength) : + writer.toString(); + } + } + return null; + } + + private void appendStackTraceElements(StackTraceElement[] elements, StringWriter writer) { + for (StackTraceElement element : elements) { + if (isNull(stackTraceIncludePrefix) || stackTraceIncludePrefix.isEmpty() || element.toString().startsWith(stackTraceIncludePrefix)) { + writer + .append("\tat ") + .append(element.toString()) + .append("\n"); + } + } + } + + private void appendStackTraceCauses(ThrowableProxy exception, StringWriter writer) { + if (addCauses) { + var cause = exception; + while (!isNull(cause.getCause())) { + cause = (ThrowableProxy) cause.getCause(); + writer + .append("caused by ") + .append(cause.getClassName()) + .append(": ") + .append(cause.getMessage()) + .append("\n"); + if (!isNull(cause.getThrowable())) { + appendStackTraceElements(cause.getThrowable().getStackTrace(), writer); + } + } } }