Loading Log4j.xml from outside fo the war

log4j

In my application iam using Log4j for logging.Presently I am placing log4j.xml in WEB-INF/classes.
Below are the configurations i am using to load log4j.xml file.

<!-- language: xml -->

    <context-param>
          <param-name>log4jConfigLocation</param-name>
         <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param>

    <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

Now I need to place log4j.xml file outside of my war file. The location will be most likely JBOSS_HOME/server/default/deploy/settings. In settings directory i need to place my log4j.xml.

I tried to load it by setting jboss class path by editing run.bat as follows
set JBOSS_CLASSPATH=%RUN_CLASSPATH%;%JBOSS_HOME%\server\default\deploy\settings
and i used below in web.xml

<!-- language: xml -->

    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:/log4j.xml</param-value>  
    </context-param>

    <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 

But it throwing exception while deploying application. Exception is
java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: class path resource [/log4j.xml] cannot be resolved to URL because it does not exist

Now my question is how can I load it.

Best Answer

Remember to add "file://" in front of the path, or else it will search inside the webapp folder.

The following example works for me.

<web-app>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>file://${MY_ENV_VAR}log4j.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
</web-app>
Related Topic