4

I've got the following lines in my log that I want to exclude, so I wanted to use a RegexFilter to do so:

[INFO ] 2018-05-20 14:52:15.993 [qtp22844606-20] TimingFilter -
Request time: 16 ms

[INFO ] 2018-05-20 14:52:18.998 [qtp22844606-17] TimingFilter -
Request time: 15 ms

[INFO ] 2018-05-20 14:52:22.001 [qtp22844606-20] TimingFilter -
Request time: 0 ms

[INFO ] 2018-05-20 14:52:24.992 [qtp22844606-17] TimingFilter -
Request time: 0 ms

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. I also tried "\bTimingFilter\b", ".*\bTimingFilter\b.*" without success.

I'm using 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. I also checked the patterns on https://regex101.com/ against my log and there they matched correctly.

1 Answer 1

5

This happens because you have useRawMsg="true" switch it to useRawMsg="false". See the log4j2 manual:

If true the unformatted message will be used, otherwise the formatted message will be used. 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. As per the log4j2 manual:

The RegexFilter allows the formatted or unformatted message to be compared against a regular expression.

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:

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:

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:

<?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". This prevents any logging from this logger.

When I run the Main class the output only contains messages from 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!
Sign up to request clarification or add additional context in comments.

2 Comments

Dear Robin, thanks a lot for your answer. I tried the suggestion, first deleting the useRawMsg="true" parameter and then also setting it explicitly to false. Unfortunately no change, still the pattern does not match... Best regards, Dirk
Dear Robin, thanks a lot for your answer, that was it. I missed the point, that the "TimingFilter" from my log entries were due to the "%c{1}" part of my pattern and did not belong to the message. So if I wanted to use RegEx I would have to go for the "Request Time" string at the beginning of the message. But your solution is even better. Thanks a ton for your help :-) Dirk PS: I tried to upvote, but due to my still low reputation, it doesn't show.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.