Application Logging in a Container Environment
Log4j, by default, uses a single logger repository. So, unless you log within the context of your own custom logger repository, the last entity to configure Log4j will have configured Log4j for every app using the default logger repository. This is not usually a concern in a standalone app, but in a containers such as Tomcat, JBoss, and Websphere, you will have to take precautions in order to keep your app's logging configuration separate from others in the container. Here are a some ways to do this...
1. In Servlet applications, put log4j.jar in WEB-INF/lib and your Log4j configuration file (log4j.properties or log4j.xml) in WEB-INF/classes of each webapp. Since each webapp should have its own classloader that other apps can't see (nor should the container be able to see it based on Java2 classloader heirarchy rules) and the WebappClassLoader loads classes and resources in a manner inverse to that of normal Java2 classloaders (looks to itself before looking at parent classloaders to load classes and resources), the default logger repository is, essentially, reserved for use by your webapp alone.
2. Use a custom logger repository selector to create a distinct logger repository for your application. This allows for log4j to be in a global classloader that can be seen by all apps (and/or the container as well), but still provide a distinct logging environment for each application. To do this, you will need to utilize an implementation of a logger repository selector; two of which exist in the log4j-sandbox project.
See:
Specifically the links to implementations the custom repository selectors and servlet stuff:
http://cvs.apache.org/viewcvs/logging-log4j-sandbox/src/java/org/apache/log4j/selector/
http://cvs.apache.org/viewcvs/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/
Take a look at the InitContextListener to utilize the repository selectors in a webapp...
The Javadoc is pretty extensive and should show you how to use everything.
You can check out the latest CVS. See instructions at http://logging.apache.org/site/cvs-repositories.html
Also see:
...which is a separate logger repository implementation for Log4j and Tomcat-3.3.x. Not yet imlemented for 5.x.x and probably won't be for 4.1.x.
Jake