log4j是否支持JSON格式?
是否可以让log4j仅通过更改配置文件以JSON格式输出其日志记录?
我使用一个使用.我只看到XML布局,但没有JSON布局。log4j.properties.xml
log4j 1.2
是否可以让log4j仅通过更改配置文件以JSON格式输出其日志记录?
我使用一个使用.我只看到XML布局,但没有JSON布局。log4j.properties.xml
log4j 1.2
这是官方的 JSON 布局
https://github.com/logstash/log4j-jsonevent-layout
1) 添加 maven 依赖 https://mvnrepository.com/artifact/net.logstash.log4j/jsonevent-layout
<dependency>
<groupId>net.logstash.log4j</groupId>
<artifactId>jsonevent-layout</artifactId>
<version>1.7</version>
</dependency>
2) 将配置添加到您的文件log4j.properties
log4j.rootCategory=WARN, RollingLog
log4j.appender.RollingLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingLog.Threshold=TRACE
log4j.appender.RollingLog.File=api.log
log4j.appender.RollingLog.DatePattern=.yyyy-MM-dd
log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1
只需使用buildin PatternLayout就可以了:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern={"debug_level":"%p","debug_timestamp":"%d{ISO8601}","debug_thread":"%t","debug_file":"%F", "debug_line":"%L","debug_message":"%m"}%n
会把像:
{
"debug_level" : "INFO",
"debug_timestamp" : "2016-05-26 16:37:08,938",
"debug_thread" : "main",
"debug_file" : "TestLogOutPut.java",
"debug_line" : "316",
"debug_message" : "hello i am a log message"
}