简体   繁体   English

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

[英]Log4j path Configuration Error In J2EE Web Application

I am facing a problem when using Log4j within my Java EE web application using DOMConfigurator. 使用DOMConfigurator在Java EE Web应用程序中使用Log4j时遇到问题。 I have log4j.jar file on my class path, and the path of the log4j.xml file /WEB-INF/config/log4j.xml . 我的类路径上有log4j.jar文件,而log4j.xml文件的路径为/WEB-INF/config/log4j.xml

Below is the exception: 以下是例外:

  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)

Below is the code getting the log4j.xml file inside a servlet: 下面是在servlet中获取log4j.xml文件的代码:

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

Entry inside web.xml web.xml中的条目

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

Move your configuration file "log4j.xml" into classpath (/WEB-INF/classes/log4j.xml) 将配置文件“ log4j.xml”移到类路径(/WEB-INF/classes/log4j.xml)

if your log4j.xml is in the classpath, when you start your app-server, it should be loaded automatically. 如果您的log4j.xml位于类路径中,则在启动应用程序服务器时,应自动加载它。

if you are still getting the path issue. 如果您仍然遇到路径问题。 Try this 尝试这个

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

   DOMConfigurator.configure(absoluteDiskPath);

Check this link as well Log4j.xml throw file not found in Java EE application 还要检查此链接, 在Java EE应用程序中找不到Log4j.xml抛出文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM