Multiple Log4J instances in a WebApp

Many times I've come across the classic problem of different logging instances in an web app server. Most of them involve classloader conflicts:
  • The webapp has log4j.jar but the webapp has bundled log4j.jar as well and you get errors.
  • OR you have the log4j.jar in the application server's shared library directory and you have 2 webapps but the configuration conflict - or even just a configuration for the application server itself and one for the webapp.

I've finally come across a solution for log4j. It involves using a repository selector based on a context, this is fully described here - Supporting the log4j RepositorySelector in Servlet Containers.

So the solution to stop conflicts and independently configure each application is this.
First put ALL logging jars in the shared library directory including (log4j.jar, slf4j-api.jar, slf4j-log4j.jar, commons-logging.jar, etc). This ensures that if they one of the libraries is loaded in the main classloader it can access the other jars.
Next is a little extension for log4j: log4j-contrib. Put its jar into the shared library as well.

Next you have to configure each individual app. Add the following to the web.xml (at the top so it is the first called).
<!-- Use from the classpath -->

If you want to have Tomcat use log4j (not just the apps), then add the following configuration:

For catalina.bat add somewhere in the file (line 119):
rem JavaUtilLogging to SLF4J required to be on classpath.
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\lib\jul-to-slf4j-1.5.6.jar;%CATALINA_HOME%\lib\slf4j-api-1.5.6.jar;%CATALINA_HOME%\lib\slf4j-log4j12-1.5.6.jar;%CATALINA_HOME%\lib\log4j-1.2.15.jar

Or under linux, add somewhere appropriate (line 163):
# JavaUtilLogging to SLF4J required to be on classpath.

Additionally if you put the in somewhere other than CATALINA_HOME/lib then you'll need to add that directory to the classpath. e.g.
rem if you put it in /conf

Now alter conf/ and replace the entire contents with:
handlers = org.slf4j.bridge.SLF4JBridgeHandler
.handlers = org.slf4j.bridge.SLF4JBridgeHandler

And that's it for the command line.

Running it from Eclipse

Eclipse has a guide how to set it up at Enable JULI logging in Eclipse. To set it up, with the configuration files stored in the workspace under Servers for a server named "Tomcat6-Localhost" add the following to the end of the Server's launch configuration arguments:
Copy in and into the workspace configuration directory as per the command line version.

Note: the argument -Dlog4j.configuration, sets the file name to search for on the Tomcat start up classpath. You CANNOT specify the exact file to load, instead you have to add the folder as a class folder (add external folder in eclipse), in order for it to be located.

Under the launch configuration classpath, add the following jars/folders:
  • jdk:lib/tools.jar
  • bin/bootstrap.ja
  • lib/jul-to-slf4j-1.5.6.jar
  • lib/slf4j-api-1.5.6.jar
  • lib/slf4j-log4j12-1.5.6.jar
  • lib/log4j-1.2.15.jar
  • workspace:Servers/Tomcat6-Localhost-config