Log4j2在apender中过滤特定级别 [英] Log4j2 Filter particular level in apender

查看:367
本文介绍了Log4j2在apender中过滤特定级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么过滤器来定义要使用apender记录的特定级别?例如:

What filter should i use, to define particular level to be logged with apender? For example:

java:

LOGGER.debug("Debug message");
LOGGER.info("Info message");
LOGGER.warn("Warn message");
LOGGER.error("Error message");
LOGGER.fatal("Fatal message");

log4j2.xml:

log4j2.xml:

<Configuration>
    <Appenders>
        <Console name="info-stdout-message">
            <PatternLayout pattern="[%logger{36}] %message %n" />
            <ThresholdFilter level="info"/>
        </Console>

        <Console name="detailed-stdout-message">
            <PatternLayout pattern="[%logger{36}] [%level] %message %n" />
        </Console>

        <File name="file-appender" fileName="logs/debug.log">
            <PatternLayout pattern="%d{HH:mm:ss dd.mm} [%t] [%-5level] %logger{36} - %msg %n" />
        </File>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="file-appender" level="debug" />
            <AppenderRef ref="info-stdout-message" level="info"/>
            <AppenderRef ref="detailed-stdout-message" level="info"/>
        </Root>
    </Loggers>
</Configuration>

文件输出正常,但在控制台我有这样的结果:

The file output is fine, but in console I have such result:

[application.Main] [INFO] Info message
[application.Main] Info message
[application.Main] [WARN] Warn message
[application.Main] Warn message
[application.Main] [ERROR] Error message
[application.Main] Error message
[application.Main] [FATAL] Fatal message
[application.Main] Fatal message

但我需要 info-stdout-message appender只输出INFO消息,而 detailed-stdout-message 输出所有EXEPT INFO。因此控制台输出应如下所示:

but I need info-stdout-message appender to output only INFO messages, while detailed-stdout-message to output all EXEPT INFO. So the console output should looks like:

[application.Main] Info message
[application.Main] [WARN] Warn message
[application.Main] [ERROR] Error message
[application.Main] [FATAL] Fatal message

无法了解如何防止过滤器尊重级别继承。是否可以这样做?

Can't find out how to prevent filters respect level inheritance. Is it possible to do this?

推荐答案

这有效:

<Console name="info-stdout-message">
    <PatternLayout pattern="[%logger{36}] %message %n" />
    <Filters>

        <!-- Now deny warn, error and fatal messages -->
        <ThresholdFilter level="warn"  onMatch="DENY"   onMismatch="NEUTRAL"/>

        <!-- This filter accepts info, warn, error, fatal and denies debug/trace -->
        <ThresholdFilter level="info"  onMatch="ACCEPT" onMismatch="DENY"/>
    </Filters>
</Console>

这篇关于Log4j2在apender中过滤特定级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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