12

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?

Harshit Gupta
  • 167
  • 1
  • 1
  • 10
  • You can find [here](https://stackoverflow.com/questions/46560939/logback-layout-pattern-in-logback-xml) the answer to a similar question. If you think that achieving what you want with Logback is cumbersome, you might want to have a look at Log4j2. [Here](https://www.baeldung.com/java-log-json-output) is some guidance. – dbaltor Sep 11 '20 at 07:01
  • Migrating to log4j2 is not feasible. We will have to change the logs in the code as well. – Harshit Gupta Sep 11 '20 at 10:08

3 Answers3

7

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:

Parth Kansara
  • 189
  • 1
  • 11
0

you lose the between and in you lagback.xml

<configuration debug="true">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    ***<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">***
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
            <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZZ</timestampFormat>
            <timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>

            <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
        </layout>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="stdout"/>
</root>
Frank
  • 1
0

As of logback 1.3.8/1.4.8 there is built in support for JSON https://logback.qos.ch/manual/encoders.html#JsonEncoder so you can just change the config

So you config file logback-spring.xml would look like

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.JsonEncoder"/>
    </appender>

    <root>
        <level value="DEBUG"/>
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

Then you will get JSON output to the console

James Mudd
  • 1,816
  • 1
  • 20
  • 25