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
From log4j documentation -
Filters may be configured in one of four locations:
- 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.
- 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.
- Appender Filters are used to determine if a specific Appender should handle the formatting and publication of the event.
- 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 -
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With