Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

java - log different log levels to different files with log4j

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...