问题
TL;DR If I'm using JsonLayout with a nested KeyValuePair in my log4j2.xml config the resulting log messages are empty. Any ideas why?
Long Story
I'm using Tomcat 8.5.43 with the following logging related JARs:
- jackson-annotations-2.9.7.jar
- jackson-core-2.9.7.jar
- jackson-databind-2.9.7.jar
- slf4j-api-1.7.25.jar
- jcl-over-slf4j-1.7.25.jar
- log4j-api-2.12.0.jar
- log4j-core-2.12.0.jar
- log4j-jul-2.12.0.jar
- log4j-slf4j-impl-2.12.0.jar
- tomcat-extras-juli-8.5.2.jar (as tomcat-juli.jar in /bin)
- tomcat-extras-juli-adapters-8.5.2.jar
- log4j-web-2.12.0.jar (in the webapp)
and the log4j2.xml config:
<?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>
In the setenv.sh I set the classpath to:
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"
And run Tomcat with the additional JVM args:
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=file:log4j2.xml"
So every Tomcat, JULI, Apache commons and Slf4j logging should be redirected to Log4J2 in the end.
Unfortunately all I see is this: {"logEvent":"","foo":"bar"}
Instead of something like this:
{"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}}
If I remove the KeyValuePair
everything works fine as expected.
What's the issue here?
回答1:
I'm experiencing the same problem. It's somehow related to log4j-2.12.0, in 2.11.2 it is OK.
来源:https://stackoverflow.com/questions/57003440/why-is-log4j2-jsonlayout-keyvaluepair-printing-empty-logevent-messages