Skip to content

Commit

Permalink
- Utvider TestnavLogbackEncoder til å ta config maxStackTraceLength (…
Browse files Browse the repository at this point in the history
…default som før).

- Endrer logging midlertidig pga. testing.

#deploy-levende-arbeidsforhold-ansettelse
  • Loading branch information
rfc3092 committed Oct 24, 2024
1 parent 5a5e414 commit 143f450
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProfile name="prod">
<appender name="stdout_json" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="no.nav.testnav.libs.reactivecore.logging.TestnavLogbackEncoder">
<!-- <appender name="stdout_json" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <encoder class="no.nav.testnav.libs.reactivecore.logging.TestnavLogbackEncoder">-->
<!-- <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">-->
<!-- <rootCauseFirst>true</rootCauseFirst>-->
<!-- -->
Expand All @@ -15,8 +15,8 @@
<!-- <exclude>org\.apache\.coyote\..*</exclude>-->
<!-- <exclude>org\.apache\.tomcat\..*</exclude>-->
<!-- </throwableConverter>-->
</encoder>
</appender>
<!-- </encoder>-->
<!-- </appender>-->
<root level="INFO">
<appender-ref ref="stdout_json"/>
</root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -14,45 +15,50 @@

import static java.util.Objects.nonNull;

/**
* Config:
* <li>{@code maxStackTraceLength}: Default 480, set to a negative number to disable truncation of stack trace altogether.</li>
*/
@Slf4j
public class TestnavLogbackEncoder extends LogstashEncoder {

// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
private final Pattern pattern = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");

@Setter
private int maxStackTraceLength = 480;

@SneakyThrows
@Override
public byte[] encode(ILoggingEvent event) {
var outputStream = new ByteArrayOutputStream();

var generator = new JsonFactory().createGenerator(outputStream);

generator.writeStartObject();

generator.writeStringField("@timestamp", new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX")
.format(new java.util.Date(event.getTimeStamp())));
generator.writeStringField("message", formatMessage(event.getFormattedMessage()));
generator.writeStringField("logger_name", event.getLoggerName());
generator.writeStringField("thread_name", event.getThreadName());
generator.writeStringField("level", event.getLevel().toString());

if (nonNull(event.getThrowableProxy())) {
var exception = (ThrowableProxy) event.getThrowableProxy();
if (nonNull(exception.getThrowable())) {
var sw = new StringWriter();
var pw = new PrintWriter(sw);
for (StackTraceElement element : exception.getThrowable().getStackTrace()) {
pw.println("\tat " + element);
var outputStream = new ByteArrayOutputStream();
try (var generator = new JsonFactory().createGenerator(outputStream)) {

generator.writeStartObject();
generator.writeStringField("@timestamp", new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX")
.format(new java.util.Date(event.getTimeStamp())));
generator.writeStringField("message", formatMessage(event.getFormattedMessage()));
generator.writeStringField("logger_name", event.getLoggerName());
generator.writeStringField("thread_name", event.getThreadName());
generator.writeStringField("level", event.getLevel().toString());
if (nonNull(event.getThrowableProxy())) {
var exception = (ThrowableProxy) event.getThrowableProxy();
if (nonNull(exception.getThrowable())) {
var sw = new StringWriter();
var pw = new PrintWriter(sw);
for (StackTraceElement element : exception.getThrowable().getStackTrace()) {
pw.println("\tat " + element);
}
var stackTrace = maxStackTraceLength < 0 ? sw.toString() : sw.toString().substring(0, maxStackTraceLength);
generator.writeStringField("stack_trace", stackTrace);
}
var stackTrace = sw.toString()/*.substring(0, 480)*/; //Limit the stack trace to 480 characters
generator.writeStringField("stack_trace", stackTrace);
}
}
generator.writeEndObject();

generator.writeEndObject();

generator.flush();
outputStream.write('\n');
generator.flush();
outputStream.write('\n');
}

return outputStream.toByteArray();
}
Expand Down

0 comments on commit 143f450

Please sign in to comment.