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.
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.