Search code examples
log4cxx

Writing different levels to different files in log4cxx


I want to have log messages from each log level go to a different file. From the name, LevelMatchFilter seems like what I want, except it seems to not filter anything from a different level.

I think the following properties should do that using LevelRangeFilter. However, anything sent to the global logger ends up in INFO.log, regardless of the level.

log4j.rootLogger = OFF

# Global level based logs
log4j.logger.global = ALL, Info

log4j.appender.Info=org.apache.log4j.FileAppender
log4j.appender.Info.File=Logs/INFO.log
log4j.appender.Info.layout=org.apache.log4j.PatternLayout
log4j.appender.Info.layout.ConversionPattern=%d [%p]  %m%n
log4j.appender.Info.filter.a=org.apache.log4j.filter.LevelRangeFilter
log4j.appender.Info.filter.a.LevelMin=info
log4j.appender.Info.filter.a.LevelMax=info
log4j.appender.Info.filter.a.AcceptOnMatch=true

I also tried using INFO for the values of LevelMin and LevelMax but that had the same results.

What am I doing wrong?

As a side question, is there a way to turn on debugging of the log4cxx configuration when using a property file? I found an option when using an xml file, but none of the obvious translations to properties (debug=true, log4j.debug=true) and any effect.


Solution

  • As of log4cxx 0.10 (and probably earlier), the properties format does not support filters. So the XML configuration (or programmatic configuration) is required.

    <?xml version="1.0" encoding="UTF-8" ?>
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
      <appender name="Info" class="org.apache.log4j.FileAppender">
        <param name="file" value="Logs/INFO.log" />
        <param name="append" value="false" />
    
        <!-- If this filter accepts the message, it will be printed.  That happens if this is an info message -->
        <filter class="org.apache.log4j.filter.LevelMatchFilter">
          <param name="levelToMatch" value="INFO" />
          <param name="acceptOnMatch" value="true" />
        </filter>
        <!-- If it is not an info message, this filter will reject it -->
        <filter class="org.apache.log4j.filter.DenyAllFilter"/>
    
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%d [%p] %m%n" />
        </layout>
      </appender>
      <root>
        <priority value="off" />
      </root>
      <logger name="global">
        <priority value="all" />
        <appender-ref  ref="Info" />
      </logger>
    </log4j:configuration>