I am using logback for logging in my spring boot application and using the pattern as per:
"%d [%thread] %-5p [%c] [%F:%L] [trace=%X{X-B3-TraceId:-},span=%X{X-B3-SpanId:-}] - %msg%n"
Now I want to move to the JSON layout for my logs. But I don't see a way to apply the pattern to my logs as a result many of the above information is lost.
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</appender>
Any alternative way to achieve the same?
I have came across the same problem after implementing the JsonLayout in my logback-spring.xml I realized I cannot put in my own custom pattern.
Well after googling a lot and wasting few hours I came across 1 more kind of logAppender which allows pattern and also print logs in Json format.
You need to use net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder encoder instead of JsonLayout
The sample implementation can be as follows:
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timestamp</fieldName>
<pattern>yyyy-MM-dd' 'HH:mm:ss.SSS</pattern>
</timestamp>
<pattern> your desired pattern </pattern>
</providers>
</encoder>
Edit: For in-details documentation you can also check the this github link:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With