Reputation: 923
I need to print logs in pretty Json format, with all details.
Currently I have my small service on spring boot and application.yml configuration file. I have below configuration for logging;
logging:
level:
root: INFO
org:
springframework:
security: INFO
web: INFO
hibernate: INFO
apache:
commons:
dbcp2: INFO
file: ../logs/myLog.log
pattern:
console: '%d{yyyy-MMM-dd HH:mm:s s.SSS} %-5level [%thread] %logger{15} - %msg%n'
Could you please advise me is there any configuration to change appender, so I could get logs in Json format.
Upvotes: 2
Views: 3933
Reputation: 4682
First create a file named logback-spring.xml in folder src/main/resources with a content similar to this one:
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<logger name="jsonLogger" additivity="false" level="DEBUG">
<appender-ref ref="consoleAppender"/>
</logger>
<root level="INFO">
<appender-ref ref="consoleAppender"/>
</root>
</configuration>
Second add the following dependency:
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.3</version>
</dependency>
And that's it.
You should see lines similar to this one:
{"@timestamp":"2018-10-11T23:48:57.215+00:00","@version":1,"message":"Sample TRACE Message","logger_name":"com.example.demo.LoggingExample","thread_name":"http-nio-8080-exec-1","level":"TRACE","level_value":5000}
See full example for more details.
Upvotes: 3