how can it be done by simply configuration of Log4j? There are some methods below.

Use the filter

You can make it though specifying the filter of certain appender.

Notes: Filter is not supported by PropertiesConfigurator,so if you want to setup you log by file, just use XML instead.

You can configurate your filter like this:

Use LevelMatchFilter and DenyAllFilter

<?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="info-out" class="org.apache.log4j.FileAppender"> 
                <param name="File" value="info.log"/> 
                <layout class="org.apache.log4j.PatternLayout"> 
                        <param name="ConversionPattern" value="%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> 

        <appender name="error-out" class="org.apache.log4j.FileAppender"> 
                <param name="Append" value="false"/>       
                <param name="File" value="error.log"/> 
                <layout class="org.apache.log4j.PatternLayout"> 
                        <param name="ConversionPattern" value="%m%n"/> 
                </layout> 
                <filter class="org.apache.log4j.varia.LevelMatchFilter">
                        <param name="LevelToMatch" value="error" />
                        <param name="AcceptOnMatch" value="true"/>
                </filter>
                <filter class="org.apache.log4j.varia.DenyAllFilter" />
        </appender> 

        <root> 
                <level value="info"/> 
                <appender-ref ref="info-out" /> 
                <appender-ref ref="error-out" /> 
        </root> 
</log4j:configuration>

OR

Use LevelRangeFilter

<?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="info-out" class="org.apache.log4j.FileAppender"> 
                <param name="File" value="info.log"/> 
                <layout class="org.apache.log4j.PatternLayout"> 
                        <param name="ConversionPattern" value="%m%n"/> 
                </layout> 
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMax" value="info"/>
                        <param name="LevelMin" value="info"/>
                        <param name="AcceptOnMatch" value="true"/>
                </filter>
        </appender> 

        <appender name="error-out" class="org.apache.log4j.FileAppender"> 
                <param name="Append" value="false"/>       
                <param name="File" value="error.log"/> 
                <layout class="org.apache.log4j.PatternLayout"> 
                        <param name="ConversionPattern" value="%m%n"/> 
                </layout> 
                <filter class="org.apache.log4j.varia.LevelRangeFilter"> 
                        <param name="LevelMax" value="error"/>     
                        <param name="LevelMin" value="error"/>
                        <param name="AcceptOnMatch" value="true"/> 
                </filter>
        </appender> 

        <root> 
                <level value="info"/> 
                <appender-ref ref="info-out" /> 
                <appender-ref ref="error-out" /> 
        </root> 
</log4j:configuration>

LogToAppenderByLevel (last edited 2012-02-04 20:47:39 by PidSter)