Java Log4j2 过滤器中的特定级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24695133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:13:46  来源:igfitidea点击:

Log4j2 Filter particular level in apender

javalogginglog4jlog4j2

提问by ovnia

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

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

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

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

但我需要info-stdout-messageappender 只输出 INFO 消息,同时detailed-stdout-message输出所有 EXEPT INFO。所以控制台输出应该是这样的:

[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?

无法找到如何防止过滤器尊重级别继承。是否有可能做到这一点?

采纳答案by Remko Popma

This works:

这有效:

<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>

回答by Heri

Here two console appenders. One logs all trace, debug and info levels to std_out, the other logs all warn, error and fatal levels to std_err. This is very useful e.g. within eclipse since std_err is displayed red.

这里有两个控制台附加程序。一个将所有跟踪、调试和信息级别记录到 std_out,另一个将所有警告、错误和致命级别记录到 std_err。这在 eclipse 中非常有用,因为 std_err 显示为红色。

<Console name="STDOUT" target="SYSTEM_OUT">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} (%6r) %-5p [%-7t] %F:%L %x - %m%n" />
    <Filters>
        <ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT" />
    </Filters>
</Console>

<Console name="STDERR" target="SYSTEM_ERR">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} (%6r) %-5p [%-7t] %F:%L %x - %m%n" />
    <Filters>
        <ThresholdFilter level="WARN" onMatch="ACCEPT" />
    </Filters>
</Console>

回答by downdrown

It took me very long to figure out how to filter a range of LogLevels via the log4j2.xml configuration file. In the End this worked for me :

我花了很长时间才弄清楚如何通过 log4j2.xml 配置文件过滤一系列 LogLevel。最后这对我有用:

<Filters>
    <ThresholdFilter level="trace" />
    <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL" />
</Filters>

In this case Log4J will log all messages from Level.TRACE to Level.DEBUG, all levels below Level.DEBUG will be ignored.

在这种情况下,Log4J 将记录从 Level.TRACE 到 Level.DEBUG 的所有消息,所有低于 Level.DEBUG 的级别都将被忽略。

回答by myset

Please check all log4j2 filtering possibilities at https://logging.apache.org/log4j/2.0/manual/filters.html

请在https://logging.apache.org/log4j/2.0/manual/filters.html检查所有 log4j2 过滤可能性

Sample configuration fragment

示例配置片段

...    
<Console name="DEFAULT" target="SYSTEM_OUT">
    <PatternLayout>
        <Pattern>[%d][%p][%c:%L:%M] - %m%n</Pattern>
    </PatternLayout>
    <Filters>
        <RegexFilter regex="(?s).*(sql01|sql02|sql03).*" onMatch="DENY" onMismatch="NEUTRAL"/>
    </Filters>
</Console>
...

回答by rgoers

Actually, from your question it looks like you don't want two different appenders, but two different patterns that are using under different circumstances. For that you should just use a PatternSelector.

实际上,从您的问题来看,您似乎不想要两个不同的 appender,而是在不同情况下使用的两种不同模式。为此,您应该只使用 PatternSelector。

    <Console name="stdout-message">
      <ScriptPatternSelector defaultPattern="[%logger{36}] [%level] %message %n">
        <Script name="BeanShellSelector" language="bsh"><![CDATA[
          if (logEvent.getLevel() == Level.INFO) {
            return "INFO";
          } else {
            return null;
          }]]>
        </Script>
        <PatternMatch key="INFO" pattern="[%logger{36}] %message %n"/>
       </ScriptPatternSelector>
    </Console>