1

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>
2
  • Welcome to SO. It's hard to help if we don't know what the behavior IS that is undesirable.
    – John B
    Sep 17, 2012 at 13:17
  • Thanks JohnB and oers. actully the problem is that i am trying to filter my messages. I want to log error messages in retailer_error.log and info log in retailer_info.log and similarly for other.
    – seeker
    Sep 17, 2012 at 13:22

1 Answer 1

5

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.