Details
Description
We are experiencing difficulty getting web applications deployed under Tomcat 6.0.x to honor per-app Log4J2 configuration, when Tomcat itself has been modified to also use Log4J2.
I will attach a modified Tomcat archive with the following steps applied:
1. Download and decompress Tomcat 6.0.36
2. Follow steps #3 and #4 at https://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j , but instead of using Log4J 1.x, use Log4J 2.0-beta4 core, api, and log4j-1.2 jars.
3. Deploy a web application to Tomcat which includes Log4J 2.0-beta4 .jar files, and includes the following in web.xml:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>${log4j.app.configurationFile}</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.core.web.Log4jContextListener</listener-class>
</listener>
4. Create two Log4J2 configuration files, one for Tomcat and one for the web app. They should use different PatternLayouts so output can be distinguished from each other.
5. Launch Tomcat, passing the following environment variable:
CATALINA_OPTS="-Dlog4j.app.configurationFile=/path/to/log4j2-webapp.xml -Dlog4j.configurationFile=/path/to/log4j2-tomcat.xml"
6. Force the web app to generate a log message.
7. View Tomcat's catalina.out file, and observe messages from the web app are output with Tomcat's configured PatternLayout.
With the attached Tomcat archive, it's simplified to :
1. Unzip tomcat-6.0.36-log4j2.tar.gz
2. Set the CATALINA_OPTS environment variable to:
CATALINA_OPTS="-Dlog4j.app.configurationFile=/path/to/tomcat-6.0.36/webapps/log4j2-tomcat/WEB-INF/log4j2.xml -Dlog4j.configurationFile=/path/to/tomcat-6.0.36/conf/log4j2.xml"
3. Run the platform-appropriate startup.[sh|bat] file
4. Access http://localhost:8080/log4j2-tomcat/ in a web browser.
/path/to/tomcat-6.0.36/logs/catalina.out will contain something like:
TOMCAT: 16:53:09.065 INFO org.apache.coyote.http11.Http11Protocol - Starting Coyote HTTP/1.1 on http-8080
TOMCAT: 16:53:09.096 INFO org.apache.jk.common.ChannelSocket - JK: ajp13 listening on /0.0.0.0:8009
TOMCAT: 16:53:09.099 INFO org.apache.jk.server.JkMain - Jk running ID=0 time=0/10 config=null
TOMCAT: 16:53:09.100 INFO org.apache.catalina.startup.Catalina - Server startup in 324 ms
TOMCAT: 16:53:23.617 WARN JSP - App message 1
The final message is from the web app, and should be prefixed with "APP" instead of "TOMCAT".
We've run this process in a debugger, and can observe Log4J2 being initialized both by Tomcat and the web app, with no errors. Still, for some reason the web application is using Tomcat's configuration instead of the supplied app-specific config file.
A mailing list suggestion was to only include Log4J2 core .jar in Tomcat (not in the web app). However, at startup, we get the "SimpleLogger" error message from the web app, as the Tomcat's version of the Log4J2 core .jar is hidden from the web app's classloader.
We're at a loss. Due to Tomcat's architecture, we seemingly must include Log4J2 core in both Tomcat and the web app. In a debugger, we can see both configuration files being loaded. However, when the application sends log messages, they end up using Tomcat's configuration.