5.1 List of filters
The logger allows you to very coolly configure message filtering. To do this, he has a couple of dozen filters with all sorts of parameters. The table below shows the most popular ones.
Filters | Description | |
---|---|---|
1 | BurstFilter | Allows you to control the frequency of messages per second for a given logging level. |
2 | CompositeFilter | Allows you to set multiple sequential filters. |
3 | DynamicThresholdFilter | Allows you to enable detailed logging if certain information is found in the log. |
4 | MapFilter | Allows you to build a complex logical expression for the filter from several parameters. |
5 | MarkerFilter | Allows you to filter messages by tags, the tag must first be added during the event logging. |
6 | RegexFilter | Allows you to set a mask - a regular expression. |
7 | StructuredDataFilter | Allows you to filter messages by the presence of certain data in them. |
8 | ThreadContextMapFilter | Allows you to manage filters based on data taken from the context of the current thread. |
9 | ThresholdFilter | Controls logging based on the log message level. |
10 | TimeFilter | Allows you to turn filters on and off at specific times. |
Below we will talk about just three of them. You can learn more about these filters on their official website .
5.2 TimeFilter
The filter TimeFilter
allows you to turn filters on and off at a certain time, it has 5 parameters:
1 | start | Logging start time in the formatHH:mm:ss |
2 | end | Logging off time in the formatHH:mm:ss |
3 | timezone | Sets the time zone. |
4 | onMatch | How to log if filter condition is true . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
5 | onMismatch | How to log if filter condition is false . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
Let's set up a filter that will turn on logging at 5am and turn it off at 5:30am every day. An example with a complete filter configuration is below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<TimeFilter start="05:00:00" end="05:30:00" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
5.3 RegexFilter
The filter RegexFilter
allows you to set a mask (regular expression) for filtering messages. This filter has 4 parameters:
1 | regex | Specifies a regular expression - a mask that is used to filter messages.HH:mm:ss |
2 | useRawMsg | The mask is applied in the message before formatting (true) or after formatting (false) |
3 | onMatch | How to log if filter condition is true . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
4 | onMismatch | How to log if filter condition is false . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
Let's set up a filter that will only allow messages containing the word codegym. An example with a complete filter configuration is below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".* codegym .*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
5.4 ThresholdFilter
The ThresholdFilter filter allows you to configure filtering by message level. It has only 3 parameters:
1 | level | Sets the name of the message logging level: ERROR , DEBUG , … |
2 | onMatch | How to log if filter condition is true . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
3 | onMismatch | How to log if filter condition is false . Maybe ACCEPT , DENY or NEUTRAL . Default NEUTRAL . |
Let's set up a filter that will only allow messages of level DEBUG
. An example with a complete filter configuration is below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
GO TO FULL VERSION