Keywords: Classloader isolation, JBoss7, JBoss 7, JBoss AS7, JBoss AS 7, log4j
I've been away from Java world for 4 years and looks like the technology is advanced and I forgot some of my knowledge. So, please forgive me and correct me if you encounter something wrong on this article.
This article is like an updated version of Jaikiran's article. Please read that article if you need to configure previous versions of JBoss AS. The strategy described on that article may still work on JBoss AS7 but I could not made it work.
JBoss uses log4j for its internal logging capabilities. So when JBoss loads up it loads log4j jar along with other jars. This is described as Automatic Dependencies on JBoss documentation. Packages listed in
Implicit module dependencies for deployments page are not isolated on EAR or WAR classloader. Thus root Logger of log4j is shared with your application's instance and it is configured using the configuration in standalone.xml file of JBoss.
We need to remove org.apache.log4j package from Automatic Dependencies list of JBoss on a Dynamic Web Project (WAR). Here is how to do it:
1. Create a jboss-deployment-structure.xml file in WEB-INF directory.
2. Edit its content like displayed below:
xml version="1.0"
encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.log4j"
/>
</exclusions>
</deployment>
</jboss-deployment-structure>
3. Put log4j-x.y.zz.jar into your WEB-INF/lib directory.
4. Add log4j.xml or log4j.properties into root of your src directory.
5. Optionally copy log4j.dtd from log4j-x.y.zz.jar for syntax checking.
Troubleshooting:
If you encounter any problems open up your standalone.xml and change logging levels to TRACE on urn:jboss:domain:logging:1.1 section. Then start your JBoss server. There will be a lot of log messages but you might find some crucial information explaining what went wrong. (i.e. searching "jboss-deployment-structure.xml" keyword in the log helped me.)
Attach source code of log4j to your project, find org.apache.log4j.LogManager.class, open it up and put a breakpoint to the class. (Just like on the image below) Upon starting the server this breakpoint must be hit twice. One for JBoss startup (possibly it will hit the breakpoint and you won't see the source code, so don't worry about it) and one for your application startup.
Happy coding ;)