2013-08-20 70 views
0

我在使用DOMConfigurator的Java EE Web應用程序中使用Log4j時遇到問題。我的類路徑上有log4j.jar文件,log4j.xml文件的路徑爲/WEB-INF/config/log4j.xml在J2EE Web應用程序中的Log4j路徑配置錯誤

以下是例外:

log4j:ERROR Could not parse file [/WEB-INF/config/log4j.xml]. 
    java.io.FileNotFoundException: D:\WEB-INF\config\log4j.xml (The system cannot find the  path specified) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(Unknown Source) 
at java.io.FileInputStream.<init>(Unknown Source) 
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source) 
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) 
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) 
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749) 
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:871) 
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755) 
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:896) 
at org.tree.egc.Utils.RequestFilter.init(RequestFilter.java:64) 
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295) 
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422) 
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115) 
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072) 
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726) 
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) 
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) 
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) 
at org.apache.catalina.core.StandardService.start(StandardService.java:525) 
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) 
at org.apache.catalina.startup.Catalina.start(Catalina.java:595) 
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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) 
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 

下面是得到一個servlet的內部的log4j.xml文件中的代碼:

String log4jpath=Config.getInitParameter("log4j");  
    DOMConfigurator.configure(log4jpath); 

web.xml中

<init-param> 
<param-name>log4j</param-name> 
<param-value>WEB-INF/config/log4j.xml</param-value> 
</init-param> 
+0

您需要提供*真正*路徑,例如,關閉servlet上下文,如果你堅持從文件系統加載它。國際海事組織(IMO)將它保留在類路徑上會更加清潔,其中Log4J默認尋找它。 –

+0

按照此線程中的說明: http://stackoverflow.com/questions/7151832/in-java-web-application-how-to-read-log4j-xml-from-web-inb-conf- location-with-s – MrSimpleMind

回答

0

移動進入你的配置文件「log4j.xml」到類路徑中(/WEB-INF/classes/log4j.xml)

如果您的log4j.xml位於類路徑中,那麼當您啓動應用程序服務器時,它應該自動加載。

如果您仍然遇到路徑問題。試試這個

ServletContext servletContext = request.getSession().getServletContext(); 
    String log4jpath=Config.getInitParameter("log4j");  
    String absoluteDiskPath = servletContext.getRealPath(log4jpath); 

    DOMConfigurator.configure(absoluteDiskPath); 

檢查此鏈接,以及 Log4j.xml throw file not found in Java EE application

+0

Hi Rakesh,我把log4j.xml放在類路徑(/WEB-INF/classes/log4j.xml)上,但仍然是同樣的問題。 – Pavnesh

+0

編輯我的答案。請檢查。 –

+0

工作精細非常感謝Rakesh – Pavnesh

相關問題