log different log levels to different files with l

2019-01-20 06:12发布

I am using log4j for logging in my applicaion. I am trying to different level logs in different files but something going wrong. Any help regrading this issue will be highly appreciated.

    <?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="DEBUG" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="TCS_patch_9/log/retailer_debug.log" />
  <param name="Threshold" value="DEBUG" />
  <param name="MaxFileSize" value="2MB"/>
  <param name="MaxBackupIndex" value="3"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
  </layout>
</appender>

<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="TCS_patch_9/log/retailer_info.log" />
  <param name="Threshold" value="INFO" />
  <param name="MaxFileSize" value="2MB"/>
  <param name="MaxBackupIndex" value="3"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
  </layout>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="TCS_patch_9/log/retailer_error.log" />
  <param name="Threshold" value="ERROR" />
  <param name="MaxFileSize" value="2MB"/>
  <param name="MaxBackupIndex" value="3"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
  </layout>
</appender>
<appender name="FATAL" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="TCS_patch_9/log/retailer_repeat_delay.log" />
  <param name="Threshold" value="FATAL" />
  <param name="MaxFileSize" value="2MB"/>
  <param name="MaxBackupIndex" value="3"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ISO8601} %-5p %c - %m%n"/>
  </layout>
</appender>

  <root> 
    <priority value ="error" /> 
    <appender-ref ref="console"/> 
    <appender-ref ref="DEBUG"/> 
    <appender-ref ref="INFO"/> 
    <appender-ref ref="ERROR"/>
    <appender-ref ref="FATAL"/> 
  </root>

</log4j:configuration>

1条回答
祖国的老花朵
2楼-- · 2019-01-20 06:28

The standard behaviour for log4j appenders is that they log all messages at their threshold level or higher, i.e. an appender with threshold INFO will log INFO, WARN, ERROR and FATAL messages but not DEBUG. If you want to log only INFO messages but not WARN and above then you need to use a LevelMatchFilter.

You will also need to set your root logger priority to DEBUG, otherwise it will only send ERROR and FATAL messages to its appenders and your DEBUG and INFO files will be empty.

查看更多
登录 后发表回答