Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback JsonLayout printing all logs on the same line

I am using JsonLayout with Spring Boot to log messages in JSON format. I only want the log messages to be logged to the console and not to a log file.

I notice that the JSON logs are logged continuously on the same line. On production this would be alright, since we would be shipping the logs to a log aggregator. But this becomes a bit difficult to analyze on local development.

Logs

{"timestamp":"2016-11-13 23:06:17.727","level":"INFO","thread":"qtp745835029-19","logger":"com.test.controller.TestController","message":"Info log:: printme 1","context":"default"}{"timestamp":"2016-11-13 23:06:17.727","level":"DEBUG","thread":"qtp745835029-19","logger":"com.test.controller.TestController","message":"Debug log:: printme","context":"default"}{"timestamp":"2016-11-13 23:06:17.727","level":"WARN","thread":"qtp745835029-19","logger":"com.test.controller.TestController","message":"Warn log:: printme","context":"default"}{"timestamp":"2016-11-13 23:06:17.727","level":"ERROR","thread":"qtp745835029-19","logger":"com.test.controller.TestController","message":"Error log:: printme","context":"default"} 

Below is the logback configuration
logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration>     <include resource="org/springframework/boot/logging/logback/base.xml" />     <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">         <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">             <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">                 <prettyPrint>false</prettyPrint>             </jsonFormatter>             <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>         </layout>     </appender>     <logger name="jsonLogger" additivity="false" level="DEBUG">         <appender-ref ref="consoleAppender"/>     </logger>     <root level="INFO">         <appender-ref ref="consoleAppender"/>     </root> </configuration> 

Am I missing something in the configuration so that they are logged on separate lines on the console.

Thanks for any help on this.

like image 271
Anoop Avatar asked Nov 13 '16 17:11

Anoop


1 Answers

You need to set appendLineSeparator option to true for ch.qos.logback.contrib.json.classic.JsonLayout. Example of this:

<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">     <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">         <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter" />         <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>         <appendLineSeparator>true</appendLineSeparator>     </layout> </appender>  <root level="debug">     <appender-ref ref="STDOUT" /> </root> 

like image 125
mvmn Avatar answered Oct 11 '22 18:10

mvmn