如果log4j jar在webApp的lib中,则Log4jConfigListener无法正常工作

时间:2017-05-29 13:18:11

标签: java tomcat logging log4j

我遇到了log4j初始化错误,同时在tomcat中部署了多个war文件。 我有Tomcat / lib中的所有依赖项,除了log4j.jar,它位于webApp的web-inf,lib文件夹中。和web-inf / classes文件夹中的log4j.properties。

由于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

请建议。我已经google了很多,有类似的帖子,但有些我的问题是如何解决这些问题的。

每个war的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中,为什么不能找到依赖项。

1 个答案:

答案 0 :(得分:0)

这就是我解决上述问题的方法。

Tomcat : PreResources tag not loading jars

此外,我的log4j和apachecommons以及所有其他jar现在已经外部化,并且不会使我的war文件变得庞大而庞大。一个常见的tomcat的context.xml加载了所有jar。