Log4j2不同级别的不同附加程序 [英] Log4j2 Different appender for different level

查看:90
本文介绍了Log4j2不同级别的不同附加程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,找不到解决方案.我要为级别信息而不是级别警告设置模式布局.如果我有INFO级别的日志,则一切正常,但如果日志为WARN级别,则会两次写入控制台(作为级别信息和级别警告).只需将特定级别的所有日志写出我们在该级别及以下级别的日志即可.

I have a little problem and can´t find a solution. I want to set pattern layout for level info another than for level warn. If I have a log in level INFO everything is OK, but if the log is levelWARN it is written out into console two times (as level info and as level warn). Simply all logs at a specific level is written out us log at that level and the level below.

我想登录到INFO级别,写出到控制台,如:"%-5level %d{dd-MM-yyyy HH:mm:ss} %msg%n"WARN级别,如"%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n".

I want to logs in level INFO write out to console like: "%-5level %d{dd-MM-yyyy HH:mm:ss} %msg%n" and level WARN like "%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n".

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>        
        <Console name="ConsoleInfo" target="SYSTEM_OUT">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} %msg%n"/>
        </Console>     
        <Console name="ConsoleWarning" target="SYSTEM_OUT">
            <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n"/>
        </Console>   
        <File name="File" fileName="logs/cli.log">
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n"/>
        </File>
    </Appenders>

    <Loggers> 
        <Root level="ALL">
            <AppenderRef ref="ConsoleInfo"/>
            <AppenderRef ref="ConsoleWarning"/>
            <AppenderRef ref="File"/>    
        </Root>
    </Loggers>
</Configuration>

推荐答案

据我了解,您希望具有WARN或更高级别(WARN,ERROR,FATAL)的日志事件仅转到"ConsoleWarning"附加程序,而不是而不是同时访问"ConsoleWarning"和"ConsoleInfo".

As I understand it you want to have log events with a level of WARN or higher (WARN,ERROR,FATAL) go to the "ConsoleWarning" appender only rather than going to both "ConsoleWarning" and "ConsoleInfo".

最简单的方法是在"ConsoleInfo"附加程序中修改过滤器配置,基本上执行相反的方法,如下所示:

The simplest way to do this would be to modify your filter configuration in your "ConsoleInfo" appender to basically do the opposite approach like this:

<Console name="ConsoleInfo" target="SYSTEM_OUT">
    <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT"/>
    <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} %msg%n"/>
</Console>   

之所以有效,是因为作为 log4j2手动状态:

This works because as the log4j2 manual states:

如果LogEvent中的级别与配置的级别相同或更具体,则此过滤器返回onMatch结果,否则返回onMismatch值.例如,如果ThresholdFilter配置为Level ERROR,并且LogEvent包含Level DEBUG,则将返回onMismatch值,因为ERROR事件比DEBUG更具体.

This filter returns the onMatch result if the level in the LogEvent is the same or more specific than the configured level and the onMismatch value otherwise. For example, if the ThresholdFilter is configured with Level ERROR and the LogEvent contains Level DEBUG then the onMismatch value will be returned since ERROR events are more specific than DEBUG.

这将使追加程序仅接受级别低于WARN的事件.

This will cause the appender to accept only events that have a level less than WARN.

另一种可能的解决方案是使用 RoutingAppender 指定每个级别的目的地.如果以这种方式进行操作,则根本不需要ThresholdFilters.还请注意,您可以通过不提供默认路由并且不提供该级别的路由来忽略特定级别的事件.例如,如果从下面的配置中删除<Route ref="ConsoleInfo" key="DEBUG" />,则所有路由事件附加器将忽略所有DEBUG事件,并且不会将其打印到控制台.配置如下:

Another possible solution would be to use a RoutingAppender to specify the destination for each level. If you do it this way then you don't need the ThresholdFilters at all. Also note that you can ignore events of specific levels by not providing a default route and not providing a route for that level. For example, if you remove <Route ref="ConsoleInfo" key="DEBUG" /> from the configuration below then all DEBUG events will be ignored by the routing appender and will not be printed to console. Here is the configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>        
        <Console name="ConsoleInfo" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} %msg%n"/>
        </Console>     
        <Console name="ConsoleWarning" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n"/>
        </Console>   
        <File name="File" fileName="logs/cli.log">
            <PatternLayout pattern="%-5level %d{dd-MM-yyyy HH:mm:ss} [%l] %msg%n"/>
        </File>
        <Routing name="Routing">
            <Routes>
                <Script name="RoutingInit" language="JavaScript"><![CDATA[
                    logEvent.getLevel();]]>
                </Script>
                <Route ref="ConsoleInfo" key="TRACE" />
                <Route ref="ConsoleInfo" key="DEBUG" />
                <Route ref="ConsoleInfo" key="INFO" />
                <Route ref="ConsoleWarning" key="WARN" />
                <Route ref="ConsoleWarning" key="ERROR" />
                <Route ref="ConsoleWarning" key="FATAL" />
            </Routes>
        </Routing>
    </Appenders>

    <Loggers> 
        <Root level="ALL">
            <AppenderRef ref="Routing"/>
            <AppenderRef ref="File"/>    
        </Root>
    </Loggers>
</Configuration>

希望这会有所帮助!

这篇关于Log4j2不同级别的不同附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆