gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:25:49 28 4
gpt4 key购买 nike

我正在使用 JsonLayout 和 Spring Boot 以 JSON 格式记录消息。我只希望将日志消息记录到控制台而不是日志文件。

我注意到 JSON 日志连续记录在同一行上。在生产中,这没问题,因为我们会将日志传送到日志聚合器。但这变得有点难以分析本地发展。

日志

{"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"}

下面是logback配置
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>

我是否缺少配置中的某些内容,以便它们在控制台上的单独行上登录。

感谢您对此的任何帮助。

最佳答案

您需要设置appendLineSeparator选项 truech.qos.logback.contrib.json.classic.JsonLayout .例如:

<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>

关于logging - Logback JsonLayout 在同一行打印所有日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40576959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com