简体   繁体   中英

How to log specific class with different levels to different appenders with LOG4J

I have a situation where I have 2 appenders, one writing to console, another to file. I'd like to log everything with DEBUG level except for one class. For this class I want to log DEBUG to console and ERROR to file.

I figured out that this kind of filtering is done by category tag, however I haven't managed to split different levels to different appenders on a specific class.

is there a way to do it?

Current configuration is the following :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
        </layout>
    </appender>

    <appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
        <param name="append" value="true"/>
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="log/server-bl2.log.%d{yyyy-MM-dd}.gz"/>
            <param name="ActiveFileName" value="log/server-bl2.log"/>
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
        </layout>
    </appender>

    <logger name="org">
        <level value="DEBUG"/>
        <appender-ref ref="console"/>
        <appender-ref ref="file-bl"/>
    </logger>

    <logger name="org.springframework" additivity="false">
        <level value="ERROR"/>
        <appender-ref ref="file-bl"/>
    </logger>

    <root>
        <priority value="DEBUG" />
        <appender-ref ref="console" />
        <appender-ref ref="file-bl" />
    </root>

</log4j:configuration>

Ok, so in the end I came up with a solution like this :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
        </layout>
    </appender>

    <appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
        <param name="append" value="true"/>
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="log/server-bl.log.%d{yyyy-MM-dd}.gz"/>
            <param name="ActiveFileName" value="log/server-bl.log"/>
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
        </layout>
    </appender>

    <logger name="org.springframework" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="console"/>
    </logger>

    <logger name="org.mybatis" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="console"/>
    </logger>    

    <root>
        <priority value="DEBUG" />
        <appender-ref ref="console" />
        <appender-ref ref="file-bl"/>
    </root>

</log4j:configuration>

The root level of logging is DEBUG and there a 2 appenders : console and file-bl. Then, there 2 exceptions : org.springframework and org.mybatis. For them only 1 appender applies - console.

Though, it's not an exact solution to the problem I described in the beginning since I don't log org.springframework and org.mybatis at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM