我遇到了log4j初始化錯誤,同時在tomcat中部署了多個war文件。 我擁有Tomcat/lib中的所有依賴項,除了位於webApp的web-inf,lib文件夾中的log4j.jar。和web-inf/classes文件夾中的log4j.properties。Log4jConfigListener無法工作,如果log4j jar在webApp的lib中
由於App1.war和App2.war都有自己的log4j,我已將log4j.jar保存在各自的Web Apps lib文件夾中。
然後我得到以下錯誤,
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
請建議。我搜索了很多,有類似的帖子,但一些如何解決我的問題沒有得到解決。
每個戰爭的web.xml就像下面,
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>SomeUniqueValue</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>file:${catalina.base}\webapps\MyAppName\WEB-INF\classes\log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
,如果我在Tomcat的/ lib文件夾移動log4j.jar代替,但隨後開始記錄所有日誌在一個日誌文件,它工作正常。
請建議,爲什麼不能找到依賴關係,如果他們在tomcat/lib中。