title: Log4jConfigListener not working if log4j jar is in webApp's lib
body: <p>I come across log4j initialization error, while deploying multiple war files in tomcat.
  I have all of the dependencies in Tomcat/lib, except log4j.jar, which is in webApp's web-inf , lib folder. and log4j.properties in web-inf/classes folder.</p>
  
  <p>Since App1.war and App2.war have their own log4j, i have kept the log4j.jar in respective web Apps lib folder. </p>
  
  <p>Then I get below error,</p>
  
  <pre><code>29-May-2017 13:15:01.871 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing log4j from [file:D:\Users\vissurve\Softwares\apache-tomcat-9.0.0.M17-windows-x64\apache-tomcat-9.0.0.M17\webapps\sho-sp-item\WEB-INF\classes\log4j.properties]
  29-May-2017 13:15:01.875 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
   java.lang.NoClassDefFoundError: org/apache/log4j/PropertyConfigurator
      at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:78)
      at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:154)
      at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:49)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4717)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
      at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
      at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.ClassNotFoundException: org.apache.log4j.PropertyConfigurator
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 16 more
  
  29-May-2017 13:15:01.887 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Shutting down log4j
  29-May-2017 13:15:01.888 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStop Exception sending context destroyed event to listener instance of class org.springframework.web.util.Log4jConfigListener
   java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
      at org.springframework.util.Log4jConfigurer.shutdownLogging(Log4jConfigurer.java:123)
      at org.springframework.web.util.Log4jWebConfigurer.shutdownLogging(Log4jWebConfigurer.java:172)
      at org.springframework.web.util.Log4jConfigListener.contextDestroyed(Log4jConfigListener.java:54)
      at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4764)
      at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5401)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:187)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
      at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
      at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 17 more
  </code></pre>
  
  <p>Please suggest. I have googled a lot, there are similar post but some how my issue does not get solved with those solutions.</p>
  
  <p>Each war's web.xml is like below,</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;webAppRootKey&lt;/param-name&gt;
      &lt;param-value&gt;SomeUniqueValue&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;file:${catalina.base}\webapps\MyAppName\WEB-INF\classes\log4j.properties&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>It works fine if I move log4j.jar in tomcat/lib folder instead but then it starts logging all logs in one log file.</p>
  
  <p>Please suggest, why can't it find the dependencies if they are in tomcat/lib.</p>