Search code examples
log4jslf4jlog4j2

log4j 2.0.2 on slf4j 1.7.7 can't find log4j.xml on startup?


I'm trying to get slf4j 1.7.7 to use log4j 2.0.2 as its implementation. I think I have all the necessary JARs in my WAR and the log4j.xml just gets dropped into the classes directory, but when my webapp starts up, it can't locate the log4j.xml. When I crack open the WAR, I can see it in the WEB-INF/classes, so it should be on the classpath. What am I missing?

meta-inf/manifest.mf
index.html
...
WEB-INF/web.xml
WEB-INF/classes/log4j.xml
...
WEB-INF/lib/log4j-api-2.0.2.jar
...
WEB-INF/lib/slf4j-api-1.7.7.jar
...
WEB-INF/lib/log4j-core-2.0.2.jar
WEB-INF/lib/log4j-slf4j-impl-2.0.2.jar
...

Jetty has this in its stderrout.log file, recording its disappointment...

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Solution

  • Well that is subtle. log4j-core-2.0.2 isn't looking for log4j.xml, its looking for log4j2.xml. The FAQ page http://logging.apache.org/log4j/2.x/faq.html has, in bold, "By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath." Renaming my file to log4j2.xml gets it found. Now I've got to make it correct.

    2014-10-24 10:34:53,542 ERROR Unknown object "property" of type org.apache.logging.log4j.core.config.Property is ignored.
    2014-10-24 10:34:53,545 ERROR Unknown object "logger" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored.
    2014-10-24 10:34:53,546 ERROR Unknown object "root" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored.