是否有可能在Log4j2中只抑制Warn消息,而允许所有其他的消息。[英] Is it possible to suppress only Warn messages in Log4j2 while allowing all others

本文是小编为大家收集整理的关于是否有可能在Log4j2中只抑制Warn消息,而允许所有其他的消息。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用log4j2在控制台和文件中记录消息.我收到了很多我不想要的警告消息.我只想看到调试和其他消息.是否可以在允许调试,错误和任何其他类型的消息时抑制警告消息?

我在堆栈溢出上查看了其他答案,但他们只说出抑制了该特定消息级别以下的所有消息的抑制.例如,如果我将日志级别更改为错误,我将自动抑制WARN调试和此级别以下的任何其他消息.我不想要这种行为.如果可能的话,我想仅抑制警告消息,同时允许此级别以下和高于此级别的消息.以下是我的log2j2配置文件.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n" />
        </Console>
        <File name="MyFile" fileName="logs/java.log" immediateFlush="false" append="true">
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

推荐答案

您可以使用https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/org/apache/logging/logging/logging/logging/core/core/core/core/filter/filter/levelrangefilter/levelrangefilter .

这是一个生成一些日志的简单类:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SomeClass {

    private static final Logger log = LogManager.getLogger();   
    
    public static void main(String[] args){
        
        if(log.isDebugEnabled())
            log.debug("This is some debug!");
        log.info("Here's some info!");
        log.warn("Warning will be rejected.");
        log.error("Some error happened!");
    }
}

这是一种基本配置,只将所有事件发送到控制台:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>

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

上述产生的输出是:

21:08:42.215 [main] DEBUG example.SomeClass - This is some debug!
21:08:42.217 [main] INFO  example.SomeClass - Here's some info!
21:08:42.217 [main] WARN  example.SomeClass - Warning will be rejected.
21:08:42.217 [main] ERROR example.SomeClass - Some error happened!

现在,我们将LevelRangeFilter添加到控制台Appender:

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
            <LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Console>

现在输出为:

21:15:26.987 [main] DEBUG example.SomeClass - This is some debug!
21:15:26.989 [main] INFO  example.SomeClass - Here's some info!
21:15:26.989 [main] ERROR example.SomeClass - Some error happened!

您可以看到警告消息未记录到控制台.

其他推荐答案

尝试以下阈值过滤器:

<Configuration status="error">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <Filters>
                <!-- Accept error and fatal messages -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
          
                <!-- Reject Warn messages -->
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>

                <!-- This filter accepts debug, trace, info, error, fatal  -->
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </Console>
        <File name="MyFile" fileName="logs/java.log" immediateFlush="false" append="true">
            <Filters>
                <!-- Accept error and fatal messages -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
          
                <!-- Reject Warn messages -->
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>

                <!-- This filter accepts debug, trace, info, error, fatal  -->
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

本文地址:https://www.itbaoku.cn/post/1575181.html

问题描述

I am using log4j2 to log messages in console and in a file. I am getting a lot of Warn message which I do not want. I only want to see Debug and other messages. is it possible to suppress Warn messages while allowing Debug, Error and any other types of messages?

I have looked at other answers here on stack overflow but they only tell about the suppression that suppresses all of the messages below that specific message level. For example, if I change my log level to Error I will automatically suppress Warn Debug and any other messages that are below this level. I do not want this behavior. I want to suppress Only Warn messages while allowing any messages below and above this level if it is possible. Below is my Log2j2 Config file.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n" />
        </Console>
        <File name="MyFile" fileName="logs/java.log" immediateFlush="false" append="true">
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

推荐答案

You can use the LevelRangeFilter to reject the log events.

Here's a simple class that generates some logs:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class SomeClass {

    private static final Logger log = LogManager.getLogger();   
    
    public static void main(String[] args){
        
        if(log.isDebugEnabled())
            log.debug("This is some debug!");
        log.info("Here's some info!");
        log.warn("Warning will be rejected.");
        log.error("Some error happened!");
    }
}

Here's a basic configuration that just sends all events to console:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>

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

The output generated by the above is:

21:08:42.215 [main] DEBUG example.SomeClass - This is some debug!
21:08:42.217 [main] INFO  example.SomeClass - Here's some info!
21:08:42.217 [main] WARN  example.SomeClass - Warning will be rejected.
21:08:42.217 [main] ERROR example.SomeClass - Some error happened!

Now we add the LevelRangeFilter to the Console appender:

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
            <LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Console>

Now the output is:

21:15:26.987 [main] DEBUG example.SomeClass - This is some debug!
21:15:26.989 [main] INFO  example.SomeClass - Here's some info!
21:15:26.989 [main] ERROR example.SomeClass - Some error happened!

As you can see the WARN message is not logged to console.

其他推荐答案

Try threshold filters as follows:

<Configuration status="error">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <Filters>
                <!-- Accept error and fatal messages -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
          
                <!-- Reject Warn messages -->
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>

                <!-- This filter accepts debug, trace, info, error, fatal  -->
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </Console>
        <File name="MyFile" fileName="logs/java.log" immediateFlush="false" append="true">
            <Filters>
                <!-- Accept error and fatal messages -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
          
                <!-- Reject Warn messages -->
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>

                <!-- This filter accepts debug, trace, info, error, fatal  -->
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d{DEFAULT}][%5p][%t][%c{1}] - %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>