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:
+ *
+ * - {@code maxStackTraceLength}: Default 480, set to <1 to disable truncation of stack trace altogether.
+ * - {@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.
+ * - {@code stackTraceIncludePrefix}: If set, only include stack trace elements that starts with the given prefix, e.g. "no.nav.testnav".
+ *
+ * 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:
+ *
+ * - {@code maxStackTraceLength}: Default 480, set to <1 to disable truncation of stack trace altogether.
+ * - {@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.
+ * - {@code stackTraceIncludePrefix}: If set, only include stack trace elements that starts with the given prefix, e.g. "no.nav.testnav".
+ *
+ * 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) {