From a685ce8d5e2379d3bc9176ff8784567a85033e32 Mon Sep 17 00:00:00 2001 From: Cato Olsen Date: Mon, 28 Oct 2024 10:08:13 +0100 Subject: [PATCH] feature/configurable-logging-causes (#3663) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Kan nå konfigurere eller slå av lengde før stack trace trunkeres. Default til 480 hvis ikke satt (som før, men legger nå på " (truncated)..." i tillegg). - Kan nå legge til causes på stack trace. Default til false hvis ikke satt (som før). - Kan nå filtrere på elementer i stack trace. Default til alle hvis ikke satt (som før). --- .../logging/TestnavLogbackEncoder.java | 103 ++++++++++++++---- .../logging/TestnavLogbackEncoder.java | 103 ++++++++++++++---- 2 files changed, 158 insertions(+), 48 deletions(-) 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 544251c47d6..67acbbe5fee 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 @@ -3,58 +3,113 @@ import ch.qos.logback.classic.spi.ILoggingEvent; 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: + * + * 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; + } - generator.writeEndObject(); - - generator.flush(); - outputStream.write('\n'); + 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"); + } + } + } - return outputStream.toByteArray(); + 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); + } + } + } } private String formatMessage(String message) { diff --git a/libs/servlet-core/src/main/java/no/nav/testnav/libs/servletcore/logging/TestnavLogbackEncoder.java b/libs/servlet-core/src/main/java/no/nav/testnav/libs/servletcore/logging/TestnavLogbackEncoder.java index 440949ff602..2158548eb68 100644 --- a/libs/servlet-core/src/main/java/no/nav/testnav/libs/servletcore/logging/TestnavLogbackEncoder.java +++ b/libs/servlet-core/src/main/java/no/nav/testnav/libs/servletcore/logging/TestnavLogbackEncoder.java @@ -3,58 +3,113 @@ import ch.qos.logback.classic.spi.ILoggingEvent; 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: + * + * 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; + } - generator.writeEndObject(); - - generator.flush(); - outputStream.write('\n'); + 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"); + } + } + } - return outputStream.toByteArray(); + 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); + } + } + } } private String formatMessage(String message) {