TL; DR 如果我在log4j2.xml配置中使用带有嵌套KeyValuePair的JsonLayout,则生成的日志消息为空。有什么想法吗?
长篇故事
我将Tomcat 8.5.43与以下与日志记录相关的JAR一起使用:
和log4j2.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="consoleappender" target="SYSTEM_OUT">
<JsonLayout compact="true" eventEol="true" stacktraceAsString="true" locationInfo="true">
<KeyValuePair key="foo" value="bar"/>
</JsonLayout>
</Console>
<Loggers>
<Root level="info">
<AppenderRef ref="consoleappender" />
</Root>
</Loggers>
</Configuration>
在setenv.sh中,我将类路径设置为:
CLASSPATH="$JAVA_HOME/lib/tools.jar\
:$CATALINA_HOME/lib/log4j-api.jar\
:$CATALINA_HOME/lib/log4j-core.jar\
:$CATALINA_HOME/lib/log4j-jul.jar\
:$CATALINA_HOME/lib/jackson-annotations.jar\
:$CATALINA_HOME/lib/jackson-core.jar\
:$CATALINA_HOME/lib/jackson-databind.jar"
并使用其他JVM参数运行Tomcat:
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=file:log4j2.xml"
因此,每个Tomcat,JULI,Apache Commons和Slf4j日志记录都应最终重定向到Log4J2。
不幸的是,我所看到的是:{"logEvent":"","foo":"bar"}
代替这样的事情:
{"thread":"main","level":"INFO","loggerName":"org.apache.catalina.startup.VersionLoggerListener","message":"Server version: Apache Tomcat/8.5.43","endOfBatch":false,"loggerFqcn":"org.apache.logging.log4j.jul.ApiLogger","threadId":1,"source":{"class":"org.apache.juli.logging.impl.Jdk14Logger","method":"log","file":"Jdk14Logger.java","line":87},"threadPriority":5,"instant":{"epochSecond":1562919500,"nanoOfSecond":701000000}}
如果我移除KeyValuePair
,一切都会按预期进行。
这是什么问题?
答案 0 :(得分:1)
我遇到了同样的问题。与log4j-2.12.0有某种关系,在2.11.2中就可以。