2016-05-28 148 views
0

我有一個如下所示的log4j配置文件,我想要做的是在控制檯輸出TRACE並將ERROR消息輸出到文件(fileerror)。但它不起作用,控制檯和文件都處於TRACE級別。 Java log4j配置文件不起作用

<?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

默認情況下,由子記錄器捕獲的所有消息都會傳播到根記錄器。這就是爲什麼你的兩個記錄儀記錄相同的信息。您可以將您的addability參數設置爲false(additivity =「false」),以防止記錄器將消息傳遞到根記錄器。或者,你可以添加額外的「門檻」參數文件的appender:

<param name="Threshold" value="ERROR" /> 

那麼它會忽略以下錯誤級別的所有消息。