本文介绍了特定级别的记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4j进行日志记录.我有一种情况,我必须针对不同的严重性使用单独的日志记录.例如,对于软件包foo,我必须在控制台中打印严重性为ERROR的消息,而我必须在日志文件中打印严重性为WARN的消息.如何为我的log4j.xml配置相同的内容.

I am using log4j for logging. I have a scenario where I have to use seperate logging for different severity. For example, for package foo I have to print messages with severity ERROR in the console whereas I have to print messages with severity WARN in log file. How can I configure my log4j.xml for the same.

推荐答案

您可以将LevelMatchFilter用于任务.同样,您也不要忘记使用另一个过滤器DenyAllFilter作为过滤器链中的最后一个过滤器.

You may use LevelMatchFilter for your task. Also you should not forget to use another filter - DenyAllFilter - as the last filter in the chain of filters.

您的log4j.xml将如下所示:

Your log4j.xml will look like this:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.SimpleLayout" />
        <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>

    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="error.log" />
        <layout class="org.apache.log4j.SimpleLayout" />
        <filter class="org.apache.log4j.varia.LevelMatchFilter">
            <param name="LevelToMatch" value="WARN" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
        <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender>

    <logger name="foo">
         <level value="WARN" />
         <appender-ref ref="consoleAppender" />
         <appender-ref ref="fileAppender" />
    </logger>
</log4j:configuration>

这篇关于特定级别的记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:03