J2EE Web应用程序中的Log4j路径配置错误

时间:2013-08-20 11:34:46

标签: java xml servlets path log4j

使用DOMConfigurator在我的Java EE Web应用程序中使用Log4j时遇到问题。我的类路径上有log4j.jar文件,log4j.xml文件的路径是 /WEB-INF/config/log4j.xml

以下是例外情况:

  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>

1 个答案:

答案 0 :(得分:0)

将配置文件“log4j.xml”移动到类路径中(/WEB-INF/classes/log4j.xml)

如果您的log4j.xml在类路径中,当您启动app-server时,它应该自动加载。

如果您仍然遇到路径问题。试试这个

   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