Log4j mixing log messages between applications into Weblogic
Autor: Rodolfo Vasconcelos | Publicado: 2011-10-20T04:48:00-07:00


I have an Web Service JWS application running in Weblogic 10.2 and its logs were being mixing with another web application running in the same server.

I thought that applications were totaly independent in the server but they are not. They share some classloaders. See picture bellow:





This problem occurs because jog4j.jar is shared between applications and it is retrieved from the same classloader.

In order to solve this issue, it is necessary that each application has its own log4j.jar packed in its .war (or .ear when applicable). It is important that all applications have its own log4j packed. If one of them does not, the problem will still persist.

Besides having log4j.jar packed, its also necessary to edit weblogic-application.xml to warn Weblogic to use log4j classes from application classloader.

Here you have an example of&nbsp;weblogic-application.xml:


&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;weblogic-application xmlns="http://www.bea.com/ns/weblogic/90" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
&lt;prefer-application-packages&gt;
&nbsp; &nbsp;&lt;package-name&gt;org.apache.log4j.*&lt;/package-name&gt;
&lt;/prefer-application-packages&gt;
&lt;/weblogic-application&gt;




If you are using ant to build your Weblogic JWS webservice with task jwsc, here you have a snippet of build.xml that you can use to pack log4j into .war and use a custom weblogic-application.xml.

log4j.jar must be placed into .war in WEB-INF/lib and weblogic-application.xml into .ear in META-INF


&lt;target name="jwsc" description="build web service"&gt;
&nbsp;&nbsp;
&nbsp; &nbsp; &lt;delete dir="${build.dir}"/&gt;
&nbsp; &nbsp; &lt;mkdir dir="${build.dir}"/&gt; &nbsp;
&nbsp;&nbsp; 
&nbsp; &nbsp; &lt;jwsc
&nbsp; &nbsp; &nbsp; &nbsp; srcdir="${webapp.dir}"
&nbsp; &nbsp; &nbsp; &nbsp; destdir="${build.dir}"&nbsp;
&nbsp; &nbsp;  verbose="on" debug="on"
&nbsp; &nbsp;  keepGenerated="no" &gt;

&nbsp; &nbsp;&nbsp; &lt;jws file="${jwsc.file}"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;compiledWsdl="${jwsc.jws.compiledWsdl}"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contextPath="${jwsc.jws.contextPath}"&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type="JAXWS" explode="false" &gt;
    &lt;/jws&gt;
&nbsp; &nbsp; &lt;/jwsc&gt;
&nbsp;&nbsp;
&nbsp; &nbsp; &lt;war destfile="${war.built}"&nbsp;update="true" &gt;
&nbsp; &nbsp; &nbsp; &nbsp; &lt;fileset dir="${webapp.dir}"&gt;
&nbsp;  &nbsp; &nbsp; &nbsp; &nbsp; &lt;include name="**/*.jar" /&gt;
&nbsp;       &lt;/fileset&gt;
&nbsp; &nbsp; &lt;/war&gt;

&nbsp; &nbsp; &lt;copy file="${service.src}/resource/weblogic-application.xml" todir="${build.dir}/META-INF/" overwrite="true" /&gt;
&nbsp;  
&nbsp;&lt;/target&gt;


It is important to know that&nbsp;${war.built}&nbsp;must point to .war file and&nbsp;${webapp.dir}&nbsp;must point to the web service source that owns WEB-INF folder, that owns lib folder, that owns log4j.jar.

Links:
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/classloading.html
https://forums.oracle.com/forums/thread.jspa?threadID=689458
http://www.coderanch.com/t/554020/Web-Services/java/adding-jar-files-building-war
http://ant.apache.org/manual/Tasks/war.html