我有一个如下的log4j配置文件,我想要做的是在控制台中输出TRACE并将ERROR消息输出到文件(fileerror)。但它不起作用,控制台和文件都处于TRACE级别。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="fileerror" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1024MB" />
<param name="maxBackupIndex" value="2" />
<param name="file" value="ExonImpact.error.log.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="ccbb.hrbeu" additivity="true">
<level value="TRACE"/>
<appender-ref ref="console" />
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="fileerror" />
</root>
</log4j:configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="fileerror" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1024MB" />
<param name="maxBackupIndex" value="2" />
<param name="file" value="ExonImpact.error.log.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="ccbb.hrbeu" additivity="true">
<level value="TRACE"/>
<appender-ref ref="console" />
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="fileerror" />
</root>
</log4j:configuration>
下面是我的log4j调试信息。
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [ccbb.hrbeu] additivity to [true].
log4j: Level value for ccbb.hrbeu is [TRACE].
log4j: ccbb.hrbeu level set to TRACE
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [ccbb.hrbeu].
log4j: Level value for root is [ERROR].
log4j: root level set to ERROR
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [append] to [false].
log4j: Setting property [maxFileSize] to [1024MB].
log4j: Setting property [maxBackupIndex] to [2].
log4j: Setting property [file] to [ExonImpact.error.log.txt].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: ExonImpact.error.log.txt, false
log4j: setFile ended
log4j: Adding appender named [fileerror] to category [root].
答案 0 :(得分:0)
默认情况下,子记录器捕获的所有消息都会传播到根记录器。这就是为什么两个记录器都记录相同的消息的原因。您可以将additivity参数设置为false(additivity =&#34; false&#34;),以防止记录器将消息传递给根记录器。或者您可以添加额外的&#34;阈值&#34;文件追加器的参数:
<param name="Threshold" value="ERROR" />
然后它将忽略低于错误级别的所有消息。