Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Log4j.xml from outside fo the war

Tags:

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.

like image 459
Narendra Avatar asked Oct 26 '10 13:10

Narendra


People also ask

What is log4j xml file?

This file is main file for log4j configuration. It has information about log levels, log appenders.

What is root in log4j xml?

the root logger that logs messages with level INFO or above in the all packages to the various destinations: console, e-mail and file, "com. foo" logger that logs messages with level WARN or above in package "com.


1 Answers

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>
like image 198
mattis Avatar answered Oct 20 '22 05:10

mattis