Log4j2 JsonTemplateLayout 问题

问题描述 投票:0回答:1

我正在尝试为此处描述的 log4j2 设置 json 日志记录:

https://logging.apache.org/log4j/2.x/manual/json-template-layout.html

但是,输出的格式与我期望的不符。这就是我得到的:

{"@version":1,"source_host":"localhost","message":"hello world","thread_name":"main","@timestamp":"2021-08-17T15:44:54.948-04:00","level":"INFO","logger_name":"com.logging.test.LoggingTest"}

一开始我创建了自己的模板,但这不起作用,所以我将其设置为文档中描述的logstash模板:

<JsonTemplateLayout eventTemplateUri="classpath:LogstashJsonEventLayoutV1.json"/>

我没有得到输出中的行号或许多其他字段。 我知道它正在获取 eventTemplateURI 字段,因为如果我将其设置为一个我知道不存在的值,那么我在启动时会遇到异常。

我正在使用 log4j-slf4j-impl,是否需要做任何特殊的事情才能使其与此配合使用?

谢谢

log4j2
1个回答
0
投票

您需要显式地将

locationInfoEnabled
设置为 true - 默认为 false。如果这不可用,log4j 只会跳过依赖于此信息的字段(行号、文件名、函数名 - 可能是您缺少的所有字段)。

根据您上面链接的文档:

toggles access to the LogEvent source; file name, line number, etc. (defaults to false set by log4j.layout.jsonTemplate.locationInfoEnabled property)

为了完整起见 - 我还将

stackTraceEnabled
设置为 true。

© www.soinside.com 2019 - 2024. All rights reserved.