是否可以在允许所有其他消息的同时仅抑制Log4j2中的警告消息

时间:2019-05-22 07:33:04

标签: log4j2

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

在堆栈溢出时,我已经查看了其他答案,但它们仅说明了抑制功能,该抑制功能会将所有低于该特定消息级别的消息抑制掉。例如,如果我将日志级别更改为“错误”,则将自动禁止“警告调试”和低于此级别的任何其他消息。我不要这种行为。我想抑制“仅警告”消息,同时尽可能允许任何高于此级别的消息。下面是我的Log2j2配置文件。

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

2 个答案:

答案 0 :(得分:2)

您可以使用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添加到控制台附加程序:

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

您会看到WARN消息未记录到控制台。

答案 1 :(得分:1)

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>