Monday, December 17, 2012

Problems reading Log4j.xml from outside web application


Problems reading Log4j.xml from outside web application


If you try to read the log4j.xml from outside the Spring application/classpath, you might get the following errors, if you are deploying a war file:

Stack trace


org.springframework.web.util.Log4jConfigListener failed: java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded.
java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded
                        at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:137)
                        at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
                        at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)


The explanation is given below:

Summary -  org.springframework.web.util.Log4jConfigListener only works if the WAR file is exploded
The same with HDIV

Configuration


log4jConfigLocation
file:/${CONFIG_LOCATION}/log4j.xml
 

Solution

To override this we need to add the following to the web.xml

<context-param></context-param>
    <param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>