1

I'm deploying a Spring 4 web application to Tomcat 8 and am trying to load the log4j configuration (log4j2.xml) via Spring so that it can be stored outside of the WAR. With this configuration in place, Spring throws a NoClassDefFoundError for a log4j1 class (org.apache.log4j.xml.DOMConfigurator) during deployment. Log4j2 however is loaded correctly if it's on the classpath. I do not have logj1 libraries explicitly in my dependencies, only log4j2.

Initializing Log4J with Spring? proposes configuring log4j in web.xml. I tried this, but log4j is not loaded at all and no logging statements are printed from the application.

Correctly using Log4jConfigurer in Spring proposes configuring a custom log4j config filename in Tomcat, but is not quite what I'm after. I need the file to be loaded from outside a WAR, given a filename.

Spring configuration loading log4j2.xml:

<!-- Load Log4j config file via Spring -->
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>${log4j.config.location}</value>
            <value>${log4j.refresh.interval}</value>
        </list>
    </property>
</bean>

Full stack trace:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'log4jInitialization' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/xml/DOMConfigurator
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:919)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1704)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/xml/DOMConfigurator
    at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
    at org.springframework.beans.factory.config.MethodInvokingBean.invokeWithTargetException(MethodInvokingBean.java:119)
    at org.springframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
roark
  • 792
  • 1
  • 12
  • 29

1 Answers1

4

EDIT: The Log4jConfigurer doesn't work with log4j2 since it depends on the (log4j1-only) DOMConfigurator class. Please remove it from your spring config.

Instead, use the mechanism log4j2 provides to control the config file location: system property "log4j.configurationFile", or alternatively see the Web Applications log4j2 manual page.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114