Configure multiple log4j instances in different contexts

The problem:
We needed to use different log4j.properties file for each context.

We have one library (directory.jar) which resides in tomcat's shared lib that uses log4j. We also have three contexts (core, admin & user contexts) that will be using log4j for logging too.

Initially, we put the log4j library in tomcat's shared lib, and specify log4j.properties for each context's web.xml. But we found out that only one context is able to initialize the log4j properly. The other two contexts doesn't seem to be able to use log4j properly.

After googling around for quite some time, I found out this post that suggests a fix for my problem. It uses log4j-contrib so that log4j can properly select the log4j.properties file based on it's own context.

So, here's the quick run-down on how to do it:-

1) Place log4j.jar in tomcat's shared lib.
2) Download log4j-contrib and place it in tomcat's shared lib as well.
3) For each context that needed to use log4j, edit their web.xml and add the following entries as first entry of their appropriate sections. First, add this context-param entry:-

<context-param>
   <param-name>log4jConfigLocations</param-name>
   <param-value>log4j.properties</param-value>
</context-param>

Then, add the listener (make it the first listener):-

<listener>
   <listener-class>com.mathieucarbou.log4j.web.ContextLoggingListener</listener-class>
</listener>

4) Edit the log4j.properties for each context as you wish. They should be under /WEB-INF/classes/log4j.properties for each context.

Walla! Now log4j is working happily like it should =)