简体   繁体   English

Logback JsonLayout 在同一行打印所有日志

[英]Logback JsonLayout printing all logs on the same line

I am using JsonLayout with Spring Boot to log messages in JSON format.我将 JsonLayout 与 Spring Boot 一起使用,以 JSON 格式记录消息。 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.我注意到 JSON 日志连续记录在同一行上。 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配置
logback-spring.xml 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.感谢您对此的任何帮助。

You need to set appendLineSeparator option to true for ch.qos.logback.contrib.json.classic.JsonLayout .您需要设置appendLineSeparator选项truech.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>

Setting prettyPrint true is one solution.设置prettyPrint true 是一种解决方案。 But there are cases where we need logs in single line like for sreaming to services like cloud watch.但是在某些情况下,我们需要单行登录,例如流式传输到云监视等服务。 Add following in layout在布局中添加以下内容

<appendLineSeparator>true</appendLineSeparator>

The above answer from @mvnm worked fine for me. @mvnm的上述回答对我来说效果很好。 Just in case, if required pretty print and new line separator, can use the following configuration.以防万一,如果需要漂亮的打印和换行分隔符,可以使用下面的配置。

<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">
            <prettyPrint>true</prettyPrint>
        </jsonFormatter>
        <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
        <appendLineSeparator>true</appendLineSeparator>
    </layout>
</appender>

<root level="debug">
    <appender-ref ref="STDOUT" />
</root>

我改用了logstah-logback-encoder ,它没有 JSONLayout 的问题。

Pipe the output of your Spring Boot application (or JSON log files) to jq using something like this:使用如下方式将 Spring Boot 应用程序(或 JSON 日志文件)的输出通过管道传输到jq

java -jar target/myapp.jar | jq -R 'fromjson?'

This will provide nice, color-highlighted, pretty-printed json that follows the output of your application.这将提供漂亮的、颜色突出的、印刷精美的 json,它遵循您的应用程序的输出。 Anything output by your application that isn't JSON will be ignored (for example: the Spring Boot banner).应用程序输出的任何非 JSON 的内容都将被忽略(例如:Spring Boot 横幅)。

You can use filters in jq to display only the INFO level message output:您可以在 jq 中使用过滤器来仅显示 INFO 级别的消息输出:

java -jar target/myapp.jar | jq -c -R 'fromjson? | select(.level="INFO") | {message}'

Or you can use filters to remove keys you don't want to see:或者您可以使用过滤器删除您不想看到的键:

java -jar target/myapp.jar | jq -c -R 'fromjson? | del(.timestamp,.thread)'

Editted : Try changing the prettyPrint to true -> <prettyPrint>true</prettyPrint>编辑:尝试将prettyPrint 更改为true -> <prettyPrint>true</prettyPrint>

You re writing the logs twice in the consoleAppender您在consoleAppender两次写入日志

   <logger name="jsonLogger" additivity="false" level="DEBUG">
        <appender-ref ref="consoleAppender"/>
    </logger>
    <root level="INFO">
        <appender-ref ref="consoleAppender"/>
    </root>

change to更改为

<logger name="jsonLogger" additivity="false" level="DEBUG"/>
<root level="INFO">
    <appender-ref ref="consoleAppender"/>
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM