1

我正在尝试通过以下方式在我的 Java EE 应用程序中使用 log4j 进行日志记录,其中我在应用程序中定义了 log4j.xml。

我正在尝试使用

   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

我已经在文件夹 src 下放置了 log4j.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>

谢谢

4

3 回答 3

1

像这样配置 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: //forum.springsource.org/showthread.php?30800-SpringMVC-XML-Log4j-Logging-not-functioning

于 2012-12-07T10:30:25.793 回答
0

最好放在src/main/resources文件夹里。

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

于 2012-12-07T10:03:48.223 回答
0

尝试改变

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

DOMConfigurator.configure( "/src/log4j.xml");
于 2012-12-07T10:05:37.910 回答