log4j:ERROR Could not parse file

18,479

As per the DOMConfigurator code the argument passed to it is treated as a File name. Instead of that try using the classpath resource

  public void configLogger()
    {
        URL u = getClass().getClassLoader().getResource("com/my/abc/test/log4j.xml");
        DOMConfigurator.configure(u);
    }
Share:
18,479
ѕтƒ
Author by

ѕтƒ

˙·٠•●♥ Ƹ̵̡Ӝ̵̨̄Ʒ ♥●•٠·˙ вє α ѕтαя ˙·٠•●♥ Ƹ̵̡Ӝ̵̨̄Ʒ ♥●•٠·˙

Updated on June 09, 2022

Comments

  • ѕтƒ
    ѕтƒ almost 2 years

    I waste a lot of time to solve this problem. but can't. may be it is simple.

    The jar file of application cant load the log4j.xml file which is the configuration file of log4j.
    Here is the code i am using

    import org.apache.log4j.xml.DOMConfigurator;
    public class LoggerConfig {
        public void configLogger()
        {
            DOMConfigurator.configure("log4j.xml");
        }
    }
    

    Its a maven project. and my dir strucure is:

    src
      main
        java
        |  com
        |  |  my
        |  |  |  abc
        |  |  |  |  test
        |  |  |  |  |  LoggerConfig.java
        resource
        |   com
        |   |  my 
        |   |  |  abc
        |   |  |  |  test
        |   |  |  |  |  log4j.xml
    

    My classpath file looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" output="target/classes" path="src/main/java"/>
        <classpathentry kind="src" output="target/classes" path="src/main/resources"/>  
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
        <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
        <classpathentry kind="output" path="target/classes"/>
    </classpath>
    

    My log4j.xml looks like

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
      <appender name="file" class="org.apache.log4j.FileAppender">
        <param name="File" value="target/test.log" />
        <param name="Append" value="false" />
        <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="[%p] [%d{HH:mm:ss.SSS}] %m%n" />
        </layout>
      </appender>
      <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <param name="Threshold" value="DEBUG" />
        <layout class="org.apache.log4j.PatternLayout">
          <!-- The default pattern: Date Priority [Category] Message\n -->
          <param name="ConversionPattern" value="[%p] [%d{HH:mm:ss.SSS}] %m%n" />
        </layout>
      </appender>
      <root>
        <priority value="INFO" />
        <appender-ref ref="console" />
        <appender-ref ref="file" />
      </root>
    </log4j:configuration>
    

    Thanks for any help..