Conflict in log4j when loading libraries for DOM? (Other implementations of dom in classpath not allowed?)

17,336

Solution 1

Probably is only here, but you are missing the closing </log4j:configuration>

And for the file you can write this:

<param name="file" value="/usr/logs/myLogfile.log"/>

or

<param name="file" value="c:/logs/myLogfile.log"/>

How are you logging in your servlet classes? Put an example of the import and the sentences

I guess you are doing something like this, isnt?:

import org.apache.log4j.Logger;

private Logger log = Logger.getLogger(name);
log.debug("Mensaje de debug");

UPDATE:

The problem is a missing dependency in the class loader when trying to parse the Log4j XML, not a problem in the Log4j configuration. The log shows that org/w3c/dom/ElementTraversal is missing. Having xercesImpl.jar already, the xml-apis.jar is needed

As stated here http://xerces.apache.org/xerces2-j/install.html the old xerces.jar contained all classes in one jar, but new distribution of xerces divides it in xercesImpl.jar and xml-apis.jar

Quoted:

xerces.jar is no longer available in the main distribution. You can still download this jar from deprecated distribution. xerces.jar is a Jar file that contains all the parser class files (i.e., it contains the intersection of the contents of xercesImpl.jar and xml-apis.jar).

To use another XML parser just remove xercesImpl.jar and add another implementation library. The problem was only the use of xercesimpl.jar without xml-apis.jar

Solution 2

I would like to share my experience with a problem very similar to this one that wasn't solved by using the answers above, maybe someone will have the same experience sometime.

In my case log4j wasn't able to parse the xml version of the log4j property file and this happened even though the project contained both xerces and xml-apis.

I turned out that when we upgraded one included package (selenium 2.28) the included xerces package was included but not visible to log4j for some reason.

I solved that by including only the xerces 2.10 and not the xml-apis.

Please note that this is a special case and you should test this as a last resort if the tips above doesn't work.

+1 to the answer above that helped me find my issue.

Another thing that helped me a lot was the -Dlog4j.debug VM argument which shows that log4j finds the log4j.xml file and more.

Share:
17,336
Cratylus
Author by

Cratylus

Updated on June 04, 2022

Comments

  • Cratylus
    Cratylus about 2 years

    I am trying to put log4j in a web application in Tomcat 7.
    I have the log4j library in the WEB-INF\lib of my web application.
    I have the following log4j.xml under WEB-INF\classes

    <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    
        <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
          <!-- Log output to Console -->
          <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
            <param name="Target" value="System.out"/> 
            <layout class="org.apache.log4j.PatternLayout"> 
              <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/> 
            </layout> 
          </appender>  
    
          <appender name="rolling" class="org.apache.log4j.RollingFileAppender"> 
            <param name="file" value="myLogfile.log"/>    
            <param name="append" value="true" />
            <param name="encoding" value="UTF-8" />  
            <layout class="org.apache.log4j.PatternLayout"> 
              <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/> 
            </layout> 
          </appender> 
    
          <!-- Log to both Console and File -->
          <root> 
            <priority value ="debug" /> 
            <appender-ref ref="console" /> 
            <appender-ref ref="rolling" />
          </root>  
    </log4j:configuration>
    

    I log e.g. in a servlet or filter that I know are loading but I can not see the output log anywhere.

    E.g.

    Logger log = Logger.getLogger(MyFilter.class);

    and in doFilter I log i.e. log.debug("logging information); log.error("logging information);

    I can not see the output in the Tomcat console as a result of the console appender and I don't see the myLogfile.log created either.

    I am not sure where is this file supposed to be created.

    I see only System.out statements from servlets and filters.

    What am I doing wrong here?

    UPDATE:
    I have in the classpath xercesImpl.jar and jaxb-impl.jar. Could this cause an issue?

    I noticed the following exception I missed earlier in Tomcat console:

    log4j:WARN Error during default initialization java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2823) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1160) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1655) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533) at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source) at org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:767) at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866) at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:773) at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483) at org.apache.log4j.LogManager.(LogManager.java:127) at org.apache.log4j.Logger.getLogger(Logger.java:117) at com.test.MyFilter.(MyFilter.java:26) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:256) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:103) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4624) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5281) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:866) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:842) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1095) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1617) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533) ... 48 more

    log4j:WARN No appenders could be found for logger (com.test.MyFilter). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  • Cratylus
    Cratylus over 12 years
    Not sure I follow you.Log4j.xml is under WEB-INF\classes.You are saying it is the wrong place to put it?
  • Cratylus
    Cratylus over 12 years
    @Aleja_Vigo: Where are these dependencies documented?
  • Aleja_Vigo
    Aleja_Vigo over 12 years
    @user384706: Edited for more explanation