Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering level of logger messages

I started using Log4j 2 today and I saw three ways to specify message level: 1. with filter.threshold 2. in appender itself 3. or in rootLogger Which way is recommended to use when specifying logger's message level?

I currently have this code as Log4j2 conf file:

status = error
dest = err
name = PropertiesConfig

property.filename = logs/log.log

filter.threshold.type = ThresholdFilter
filter.threshold.level = trace

appender.console.type = Console
appender.console.name = ConsoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%t.%-15c{1}] [%p] %d{HH:mm:ss.SSS} - %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = trace

appender.randomAccessFile.type = RandomAccessFile
appender.randomAccessFile.name = File
appender.randomAccessFile.filename = logs/log.log
appender.randomAccessFile.immediateFlush = false
appender.randomAccessFile.append = false
appender.randomAccessFile.layout.type = PatternLayout
appender.randomAccessFile.layout.pattern = %-10[%t.%c{1}] [%p] %d{HH:mm:ss.SSS} - %m%n
appender.randomAccessFile.filter.threshold.type = ThresholdFilter
appender.randomAccessFile.filter.threshold.level = trace

rootLogger.level = trace
rootLogger.includeLocation = false
rootLogger.appenderRef.stdout.ref = ConsoleLogger
rootLogger.appenderRef.file.ref = File

As you can see I have levels in 3 places. I think those appender filters are for those specific logging inteface and rootLogger.level for global filter, but what this code do? That's the default value or what?

filter.threshold.type = ThresholdFilter
filter.threshold.level = trace
like image 359
Jump3r Avatar asked Oct 18 '22 00:10

Jump3r


1 Answers

From log4j documentation -

Filters may be configured in one of four locations:

  1. Context-wide Filters are configured directly in the configuration. Events that are rejected by these filters will not be passed to loggers for further processing. Once an event has been accepted by a Context-wide filter it will not be evaluated by any other Context-wide Filters nor will the Logger's Level be used to filter the event. The event will be evaluated by Logger and Appender Filters however.
  2. Logger Filters are configured on a specified Logger. These are evaluated after the Context-wide Filters and the Log Level for the Logger. Events that are rejected by these filters will be discarded and the event will not be passed to a parent Logger regardless of the additivity setting.
  3. Appender Filters are used to determine if a specific Appender should handle the formatting and publication of the event.
  4. Appender Reference Filters are used to determine if a Logger should route the event to an appender.

In simple words, below is the filter sequence in which log messages are filtered -

  1. Context-wide filters
  2. Level declared at logger level
  3. Appender filter

If level is becoming specific in lower sequences, log messages are filtered again. e.g. If level set at context-wide filter is WARN and level set at appender filter is ERROR, then log messages with ERROR log level will get logged.

If level is becoming generic in lower sequences, level set at higher sequence is used. e.g. If level set at context-wide filter is WARN and level set at logger level is INFO, then log messages with WARN log level will be forwarded to appender.

Now, with above explanation, you can understand that each filter sequence has its own scope of control. With a small configuration file, you may be thinking it is not very useful but it would be very useful where you have a large configuration file with multiple appenders and loggers.

So, there is no recommended configuration. Given your configuration, I would prefer to remove context-wide filters and appender filters. Instead create a Logger and control log level using Logger. RootLogger level is generally set to WARN or ERROR for avoiding detailed logging of application libraries.

like image 100
Vikas Sachdeva Avatar answered Oct 20 '22 23:10

Vikas Sachdeva