在Java EE应用程序中没有找到Log4j.xml抛出文件[英] Log4j.xml throw file not found in Java EE application

本文是小编为大家收集整理的关于在Java EE应用程序中没有找到Log4j.xml抛出文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试以以下方式在应用程序中定义log4j.xml的Java EE应用程序中登录.

我正在尝试使用

访问它
   DOMConfigurator.configure( "src/log4j.xml");
   logger.warn( "Login Process has started a warning message" );

,但它一直在扔下以下错误

**编辑错误消息

log4j:ERROR Could not parse file [src/log4j.xml].
 java.io.FileNotFoundException: D:\src\log4j.xml (The system cannot find the path s     pecified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)

当我提供绝对路径时,一切都在起作用.

那么实现这一目标的方法是什么?我正在使用1.2.16.JAR版本的log4j

我每次构建应用程序

时都会关注警告
log4j:WARN No appenders could be found for logger     (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

编辑: 应用程序

Myapp
  src
  conf
  target

我已经在文件夹下的log4j.xml parc.xml

log4j.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
</appender>
   <appender name="Log" class="org.apache.log4j.FileAppender">
    <param name="File" value="Web.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %-5p %-15c{1}: %m%n" />
    </layout>
</appender>

<logger name="com.web" additivity="false">
    <level value="info" />
    <appender-ref ref="Log" />
</logger>

</log4j:configuration>

谢谢

推荐答案

最好将其放入src/main/resources文件夹中.

您也可以将其重命名为log4j.properties,具体取决于您的文件格式.
而且您应该具有log4j.jar,或者如果您使用的是Maven添加依赖项到pom.xml文件.

其他推荐答案

配置像这样的web.xml文件.

   <!-- these are at the top of the xml configuration file -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/conf/log4j.xml</param-value>
    </context-param>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>api.root</param-value>
    </context-param> 

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- ... -->

这可能会为您提供帮助: http:> http:> http:>//forum.springsource.org/showthread.php?30800-springmvc-xml-log4j-log4j-logging-not-functioning

其他推荐答案

尝试更改

DOMConfigurator.configure( "src/log4j.xml");

to

DOMConfigurator.configure( "/src/log4j.xml");

本文地址:https://www.itbaoku.cn/post/1575097.html

问题描述

I am trying to log using log4j in my Java EE application in the following way where i have defined a log4j.xml in the application.

I am trying to access it using

   DOMConfigurator.configure( "src/log4j.xml");
   logger.warn( "Login Process has started a warning message" );

but it keeps throwing the following error

** edited error message

log4j:ERROR Could not parse file [src/log4j.xml].
 java.io.FileNotFoundException: D:\src\log4j.xml (The system cannot find the path s     pecified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)

When i provided the absolute path everything is working .

So whats the way to achieve this? I am using 1.2.16.jar version of log4j

and i get following warning each time i build the app

log4j:WARN No appenders could be found for logger     (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Edited: Application struture

Myapp
  src
  conf
  target

and i have palced log4j.xml under folder src

log4j.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
    </layout>
</appender>
   <appender name="Log" class="org.apache.log4j.FileAppender">
    <param name="File" value="Web.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %-5p %-15c{1}: %m%n" />
    </layout>
</appender>

<logger name="com.web" additivity="false">
    <level value="info" />
    <appender-ref ref="Log" />
</logger>

</log4j:configuration>

Thanks

推荐答案

Better put it inside src/main/resources folder.

You can also rename it to log4j.properties, depending on your file format.
And you should have either log4j.jar, or if you are using maven add dependencies to pom.xml file.

其他推荐答案

configure web.xml file like this.

   <!-- these are at the top of the xml configuration file -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/conf/log4j.xml</param-value>
    </context-param>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>api.root</param-value>
    </context-param> 

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- ... -->

This may help you : http://forum.springsource.org/showthread.php?30800-SpringMVC-XML-Log4j-Logging-not-functioning

其他推荐答案

Try changing

DOMConfigurator.configure( "src/log4j.xml");

To

DOMConfigurator.configure( "/src/log4j.xml");