2

我正在使用 log4j-1.2.17 jar 版本,

启动应用程序时,我在控制台中收到以下错误

log4j:WARN Continuable parsing error 38 and column 24
log4j:WARN Attribute "ref" is required and must be specified for element type "appender-ref".
log4j:WARN Continuable parsing error 47 and column 12
log4j:WARN The content of element type "root" must match "(param*,(priority|level)?,appender-ref*)".
log4j:ERROR No appender named [] could be found.

这是我编辑后的 ​​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 class="org.apache.log4j.RollingFileAppender" name="FILE">
        <param value="D:\\Two\\test.log" name="File"/>
        <param value="10" name="MaxBackupIndex"/>
        <param value="200MB" name="MaxFileSize"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param value="%d[%t] %-5p(%F:&lt;%M&gt;:%L)- %m%n" name="ConversionPattern"/>
        </layout>
    </appender>





 <appender class="com.MyAppender" name="customAppen">
      <layout class="org.apache.log4j.PatternLayout">
            <param value="%d[%t] %-5p(%F:&lt;%M&gt;:%L)- %m%n" name="ConversionPattern"/>
        </layout>
    </appender>




 <appender class="org.apache.log4j.AsyncAppender" name="asynchapp">
  <param name="Blocking" value="false"/> 
    <appender-ref ref="customAppen"/>

    </appender>


    <root level="INFO">


        <appender-ref ref="FILE"/>

        <appender-ref ref="asynchapp"/>
    </root>

</log4j:configuration>
4

2 回答 2

2

这些似乎源于<appender-ref/>正下方的那条线<root>。应该将其删除,因为它不引用任何附加程序,并且您应该将“customAppen”附加程序的附加程序引用向下移动到根标记中。

于 2013-06-18T18:41:58.137 回答
0

关于其他读者的问题,我也认为顺序很重要,所以需要保持这个顺序:

renderer*, appender*,(category|logger)*,root?, categoryFactory? 

一个示例 log4j.xml 文件看起来像这样,

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

    <!-- I created a renderer for the servlet request object so that it --> 
    <!-- is easy to log info about it.  It's also reusable this way in all 
        servlets. --> 
    <renderer renderedClass="javax.servlet.http.HttpServletRequest" 
        renderingClass="com.firstpac.etools.log.HttpServletRequestRenderer"/> 

    <!-- All output goes to eTools logfile ($TOMCAT_HOME/logs directory) --> 
    <appender name="log" class="org.apache.log4j.DailyRollingFileAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
        </layout> 
    </appender> 

    <!-- All output goes to DB for client statistics --> 
    <appender name="clientStats" class="org.apache.log4j.jdbc.JDBCAppender"> 
    </appender> 


    <!-- This is our logger for the fpcServlets application. It logs all --> 
    <!-- info to files (error/debug...) --> 
    <logger name="com.firstpac.etools.log4j.log"> 
        <level value="debug" /> 
        <appender-ref ref="log" /> 
    </logger> 

    <!-- This is our logger for the fpcServlets application. It logs all --> 
    <!-- statistics info for the user --> 
    <logger name="com.firstpac.etools.log4j.clientStats"> 
        <level value="info" /> 
        <appender-ref ref="clientStats" /> 
    </logger> 

</log4j:configuration> 

源 Appache 日志记录论坛

于 2015-10-23T14:33:09.150 回答