Logback (non-access) Spring logs with custom JSON format and MDC

Elvis Ciotti
2 min readJan 6, 2023

--

In case you want to output service (non-access) logs (SLF4 API format) in JSON format, you can use the standard logstash encoder. Here is the traditional Spring config where (on prod/default mode), MDC is also automatically added as a field.

<!-- logback-spring.xml -->
<configuration>
<springProfile name="dev">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
</springProfile>
<springProfile name="test">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
</springProfile>

<springProfile name="default">
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeMdc>true</includeMdc>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</springProfile>
</configuration>

With this config, you’ll have JSON entries where MDC values are automatically added as root JSON field.

To customise the format, one option is using the LoggingEventCompositeJsonEncoder. In the example below I’m adding almost all the default fields, as well as structured arguments (passed the log method), as well as a custom JSON snippet where I decide the structure and put an MDC field (“abc”) in a nested position.

<!-- logback-spring.xml -->
<configuration>

.. dev and test as above ...

<springProfile name="default">
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp/>
<version/>
<message/>
<loggerName/>
<threadName/>
<logLevel/>
<mdc/>
<!-- repeats log arguments as root json fields -->
<arguments/>
<pattern>
<pattern>
{
"myField": {
"mySubField": "%mdc{abc}"
},
"myField2": "%mdc{abc}"
}
</pattern>
<omitEmptyFields>true</omitEmptyFields>
</pattern>
<logstashMarkers/>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</springProfile>
</configuration>

Note that I’m not printing the missing values (MDC in this case) so that I can use markers (first argument in the `log()` method) so that I’m able to override those values from the code potentially. This page contains all the references and naming.

Clap if useful, and follow me for more.

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

Responses (1)