In some ELK logging context you’ll want to have you log message formated as Json.
Since Spring use Slf4J
+ LogBack
as default logging engine, this blog post explain you hos to Json log using Slf4j and Logback.
First of all you’ll need to add the following dependency to you project.
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
Other needed dependencies are provided by Spring.
Her is an working logging configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="fase">
<springProperty scope="context" name="appRootLogLevel" source="logging.level" defaultValue="TRACE" />
<springProperty scope="context" name="appName" source="spring.application.name" defaultValue="-" />
<springProperty scope="context" name="componentName" source="spring.application.component-name" defaultValue="-" />
<springProperty scope="context" name="envName" source="spring.application.env" defaultValue="-" />
<springProperty scope="context" name="subEnvName" source="spring.application.sub-env-name" defaultValue="-" />
<springProperty scope="context" name="version" source="info.app.version" defaultValue="No Version"/>
<appender name="jsonSTDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<logstashMarkers />
<pattern>
<pattern>
{
"@timestamp": "%date{yyyy-MM-dd'T'HH:mm:ss.SSSZ}",
"@level": "%level",
"@message": "%message",
"@envname": "${envName}",
"@appname": "${appName}",
"@componentname": "${componentName}",
"@scp": "%logger{40}",
"@userid": "%mdc{userId}",
"@version": "${version}",
"@exception": "%exception{full}",
"@jsondata": "#asJson{%mdc{data}}"
}
</pattern>
</pattern>
<stackTrace>
<fieldName>@st</fieldName>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<maxLength>4096</maxLength>
<shortenedClassNameLength>20</shortenedClassNameLength>
<exclude>^sun.reflect..*.invoke</exclude>
<exclude>^net.sf.cglib.proxy.MethodProxy.invoke</exclude>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</stackTrace>
</providers>
</encoder>
</appender>
<root level="${appRootLogLevel}">
<appender-ref ref="jsonSTDOUT" />
</root>
</configuration>
Pattern explanation :
{
"@timestamp": "%date{yyyy-MM-dd'T'HH:mm:ss.SSSZ}",
"@level": "%level", #standard-fields
"@message": "%message", #standard-fields
"@envname": "${envName}",
"@appname": "${appName}",
"@componentname": "${componentName}",
"@scp": "%logger{40}",
"@userid": "%mdc{userId}", #mdc-fields
"@version": "${version}",
"@exception": "%exception{full}",
"@jsondata": "#asJson{%mdc{data}}" #pattern-json-provider
}
For some more informations you should read the developer page Logfellow :: logstash-logback-encoder
Source of this post : https://stackoverflow.com/questions/75523669/multiple-slf4j-bindings-unable-to-add-log4j2-spring-boot/75523904#75523904
Leave a Reply