Sorting Log Messages By Level

This example demonstrates how to get log messages into different log files based on the level of the log message.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
    <appender name="std-out" class="org.apache.log4j.ConsoleAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/> 
        </layout> 
    </appender> 

    <appender name="fatal-out" class="org.apache.log4j.FileAppender"> 
        <param name="File" value="d:/logs/fatal_msgs.log"/> 
        <param name="Threshold" value="fatal"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/> 
        </layout> 
    </appender> 
                
    <root> 
        <level value="warn"/> 
        <appender-ref ref="std-out" /> 
        <appender-ref ref="fatal-out" /> 
    </root> 
</log4j:configuration>

The above xml configuration file will output all warn or above log messages to the "std-out" appender. All fatal level messages will also be logged to the "fatal-out" appender. It accomplishes this by using the "threshold" property of appenders. By setting the threshold value, only log messages matching the threshold setting or above will be logged. So, setting the threshold to warn would allow log messages of levels warn, error, and fatal.

If you want limit the log messages to a single level, then you can also use a supplemental filter on the appender.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/> 
        </layout> 
    </appender> 

    <appender name="info-out" class="org.apache.log4j.FileAppender"> 
        <param name="File" value="d:/logs/info_msgs.log"/> 
        <param name="Threshold" value="info"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/> 
        </layout> 
        <filter class="org.apache.log4j.varia.LevelMatchFilter">
            <param name="LevelToMatch" value="info"/> 
            <param name="AcceptOnMatch" value="true"/> 
        </filter>     
        <filter class="org.apache.log4j.varia.DenyAllFilter"/> 
    </appender> 
        
    <root> 
        <level value="warn"/> 
        <appender-ref ref="std-out" /> 
        <appender-ref ref="info-out" /> 
    </root> 
</log4j:configuration>

The above example will log all message warn or above to "std-out", while only messages of level info will be logged to "info-out". A filter chain is configured on the "info-out" appender using the LevelMatchFilter and DenyAllFilter classes. Log messages of level info are "accepted" and logged and all other levels are "denied" and ignored. You can replicate this pattern to accept other levels in other appenders. If you want to log messages of varying levels, you can also use the LevelRangeFilter class.

LogByLevel (last edited 2012-02-04 20:51:01 by cpc10-lewi14-2-0-cust355)