我在運行於Tomcat 8.0.12的servlet 3.0 Web應用程序中使用log4j2(2.0.2)。我試圖使用weblookup來獲取Web應用上下文路徑作爲日誌文件名稱的一部分。 (我們使用屬性來配置它,在幾個不同的上下文中運行相同的war文件)。Log4j2 Web查看
的log4j2.xml文件包括
<RollingFile name="Web" fileName="/srv/dnsnet/logs/web-${web:contextPath}.logfile" filePattern="/srv/dnsnet/logs/web-${web:contextPath}-%d{yyyy-MM-dd}-%i.log.gz" append="true" immediateFlush="false">
<PatternLayout>
<Pattern>%d %-5p [%25.25t] %40.40c{3.} %15X{sysid} %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="8"/>
</RollingFile>
但網站:contextPath中沒有得到解決。我的web應用程序中有log4j-web-2.0.2.jar。我向WebLookup類添加了一些消息,問題是LoggerContext的外部上下文爲空。
所以我試着看看LoggerContext的創建地點。調用LoggerContext構造函數時的堆棧跟蹤是
at org.apache.logging.log4j.core.LoggerContext.<init>(LoggerContext.java:114)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:113)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:82)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:269)
at org.apache.logging.log4j.jcl.LogFactoryImpl$PrivateManager.getContext(LogFactoryImpl.java:108)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getLoggersMap(LogFactoryImpl.java:52)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:43)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:75)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
at org.apache.myfaces.webapp.AbstractMyFacesListener.<clinit>(AbstractMyFacesListener.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151)
at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1231)
at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:647)
at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:110)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5164)
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.deployDescriptor(HostConfig.java:581)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1686)
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
但是,ClassLoaderContextSelector始終爲外部上下文傳遞null。所以看起來好像我錯過了一些東西。
任何人都可以建議我做錯了什麼,或失蹤?我是否必須指定不同的ContextSelector?
謝謝。我無法改變真正的應用程序,因爲它很大,並且具有我無法控制的各種依賴性。但我會嘗試創建一個簡單的測試應用來調查您的建議。 – 2014-10-10 11:16:42
我的簡單應用程序也無法正常工作。 webapp中唯一的jar是log4j-core,log4j-api和log4j-web。 – 2014-10-10 15:52:28
我也發現,在Tomcat 7.0.55下,我的主應用程序最初確實解決了weblookup,但是直到它記錄org.apache.myfaces.webapp.AbstractFacesInitializer「ServletContext'xyz'initialized」時。之後,weblookup失敗。但在Tomcat 8.0.12下,它永遠不會工作。仍然在挖掘...... – 2014-10-10 15:58:06