Skip to content

Instantly share code, notes, and snippets.

@anandsunderraman
Last active February 2, 2024 02:21
Show Gist options
  • Save anandsunderraman/4c06901e6ec942dc4326035f8e0098d5 to your computer and use it in GitHub Desktop.
Save anandsunderraman/4c06901e6ec942dc4326035f8e0098d5 to your computer and use it in GitHub Desktop.
logback configuration for structured logging
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender class="ch.qos.logback.core.ConsoleAppender" name="stdout">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [appName:%thread:%X{X-B3-TraceId}:%X{X-B3-SpanId}] %logger{40} - %msg%n
</pattern>
</encoder>
</appender>
<appender class="ch.qos.logback.core.ConsoleAppender" name="jsonstdout">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<providers>
<timestamp>
<timeZone>EST</timeZone>
</timestamp>
<pattern>
<pattern>
{
"level": "%level",
"service": "appName",
"traceId": "%X{X-B3-TraceId:-}",
"spanId": "%X{X-B3-SpanId:-}",
"thread": "%thread",
"class": "%logger{40}",
"message": "%message"
}
</pattern>
</pattern>
<stackTrace>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<maxLength>2048</maxLength>
<shortenedClassNameLength>20</shortenedClassNameLength>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</stackTrace>
</providers>
</encoder>
</appender>
<springProfile name="LOCAL">
<root level="info">
<appender-ref ref="stdout"/>
</root>
</springProfile>
<springProfile name="!LOCAL">
<root level="info">
<appender-ref ref="jsonstdout"/>
</root>
</springProfile>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment