简体   繁体   English

Log4j2 RegexFilter 模式不匹配

[英]Log4j2 RegexFilter pattern does not match

I've got the following lines in my log that I want to exclude, so I wanted to use a RegexFilter to do so:我的日志中有以下几行要排除,因此我想使用RegexFilter来执行此操作:

[INFO ] 2018-05-20 14:52:15.993 [qtp22844606-20] TimingFilter - Request time: 16 ms [信息] 2018-05-20 14:52:15.993 [qtp22844606-20] TimingFilter - 请求时间:16 毫秒

[INFO ] 2018-05-20 14:52:18.998 [qtp22844606-17] TimingFilter - Request time: 15 ms [信息] 2018-05-20 14:52:18.998 [qtp22844606-17] TimingFilter - 请求时间:15 毫秒

[INFO ] 2018-05-20 14:52:22.001 [qtp22844606-20] TimingFilter - Request time: 0 ms [信息] 2018-05-20 14:52:22.001 [qtp22844606-20] TimingFilter - 请求时间:0 毫秒

[INFO ] 2018-05-20 14:52:24.992 [qtp22844606-17] TimingFilter - Request time: 0 ms [信息] 2018-05-20 14:52:24.992 [qtp22844606-17] TimingFilter - 请求时间:0 毫秒

My config looks as such:我的配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders> 
        <Console name="console" target="SYSTEM_OUT">
            <RegexFilter regex=".*TimingFilter.*" useRawMsg="true" onMatch="DENY" onMismatch="ACCEPT"/>
            <PatternLayout   pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info" additivity="false">
            <appender-ref ref="console" />
        </Root>
    </Loggers>
</Configuration>

When switching "ACCEPT" and "DENY" no message gets through, so the RegexFilter per se seems to get picked up.当切换“接受”和“拒绝”时,没有消息通过,因此 RegexFilter 本身似乎被拾取。 I also tried "\\bTimingFilter\\b", ".*\\bTimingFilter\\b.*" without success.我也试过 "\\bTimingFilter\\b", ".*\\bTimingFilter\\b.*" 没有成功。

I'm using Log4J 2.11.我正在使用 Log4J 2.11。

Can you give any hints what I do wrong?你能给出任何提示我做错了什么吗? I checked all other RegexFilter related questions here and also other sites (that's where I got the suggestions for the Regex Patterns I tried), but nothing seems to work.我检查了所有其他与RegexFilter相关的问题here以及其他网站(这是我获得有关我尝试过的 Regex 模式的建议的地方),但似乎没有任何效果。 I also checked the patterns on https://regex101.com/ against my log and there they matched correctly.我还根据我的日志检查了https://regex101.com/上的模式,它们匹配正确。

This happens because you have useRawMsg="true" switch it to useRawMsg="false".发生这种情况是因为您将useRawMsg="true"切换为 useRawMsg="false"。 See the log4j2 manual :请参阅log4j2 手册

If true the unformatted message will be used, otherwise the formatted message will be used.如果为 true,将使用未格式化的消息,否则将使用格式化的消息。 The default value is false.默认值为假。

EDIT:编辑:

I didn't look closely enough at your configuration and your output, so I apologize for that.我没有仔细查看您的配置和输出,因此我对此深表歉意。 The reason the RegexFilter does not work for you is that you're using it to try to filter based on the name of the logger . RegexFilter对您不起作用的原因是您正在使用它来尝试根据logger名称进行过滤。 As per the log4j2 manual :根据log4j2 手册

The RegexFilter allows the formatted or unformatted message to be compared against a regular expression. RegexFilter 允许将格式化或未格式化的消息与正则表达式进行比较。

To prevent a logger from logging any messages you have several options.要防止记录器记录任何消息,您有多种选择。 I will illustrate one of them with some sample code below.我将在下面用一些示例代码来说明其中之一。

Here is a main class that runs a TimingFilter class:这是一个运行TimingFilter类的主类:

package example;

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

public class Main {

private static final Logger log = LogManager.getLogger();

    public static void main(String[] args){
        log.info("Here's some info!");
        log.error("Some erorr happened!");
        TimingFilter.main(null);
    }

}

Here is the TimingFilter class:这是 TimingFilter 类:

package example;

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

public class TimingFilter {

    private static final Logger log = LogManager.getLogger();

    public static void main(String[] args){
        log.info("Here's some info!");
        log.error("Some erorr happened!");
    }
}

Here is a sample log4j2.xml file:这是一个示例 log4j2.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders> 
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout   pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger level="off" name="example.TimingFilter">
        </Logger>
        <Root level="info" additivity="false">
            <appender-ref ref="console" />
        </Root>
    </Loggers>
</Configuration>

Notice how I have configured the example.TimingFilter logger so that its level is "off".请注意我如何配置example.TimingFilter记录器,使其级别为“关闭”。 This prevents any logging from this logger.这会阻止来自此记录器的任何日志记录。

When I run the Main class the output only contains messages from Main :当我运行Main类时,输出仅包含来自Main消息:

[INFO ] 2018-05-22 23:23:30.473 [main] Main - Here's some info!
[ERROR] 2018-05-22 23:23:30.474 [main] Main - Some erorr happened!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM