How to configure log4j in your ear ?

JBoss recipe of the day


Have you got exceptions when tried to configure log4j at application level ? this is due to the fact that JBoss ships already with log4j classes:  you need to isolate the classes from other deployments. Without isolation, you can see common errors like ClassCastException, IllegalAccessErrors, VerifyErrors and in general, strange behavior.
 
Here's how to do it:
Create a log4j.properties file and package it with your application and make it visible to JBoss classpath (see image at the bottom). The log4j.properties has just a minimal content: 


log4j.logger.com.sample=TRACE

 

 

Within your application you need to add the appropriate version of log4j jar (this is important  because you will have to ensure that log4j initializes separately for your application and picks up the log4j.properties file). Place this log4j jar file in the lib folder of your EAR (as depicted in the picture).

Finally you need to  enable classloader isolation for your application. In order to achieve classloader isolation you need to add an entry in jboss-app.xml file and place it in the META-INF folder of the EAR. The jboss-app.xml looks like:

<jboss-app>
  <loader-repository>
 org.myapp:loader=MyClassLoader
     <loader-repository-config>java2ParentDelegation=false </loader-repository-config>
   </loader-repository>
 </jboss-app>

This is how your application EAR should look like:
jboss log4j

Follow us on Twitter