Log4j : Multiple loggers, levels and appenders

I am having trouble with duplicate log messages when writing to multiple log files using log4j.

At present I am trying to log INFO level data (and upwards) for the specific logger named foobar in my foo.log file and then all WARN level log messages (and upwards) for all loggers in the bar.log file.

As a result of this, duplicate log messages were written to the foo.log file (each line was logged twice) and after some quick research I found that the suggestion to fix this was to add log4j.additivity.foobar=false to my properties file.

The problem with this is that although it stops duplicate lines, the WARN message from the foobar logger are never written to the bar.log file.

My log4j properties file is as follows:

log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO, FOO
log4j.additivity.foobar = false

log4j.appender.FOO = org.apache.log4j.RollingFileAppender
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.FOO.File = foo.log

log4j.appender.BAR = org.apache.log4j.RollingFileAppender
log4j.appender.BAR.layout = org.apache.log4j.PatternLayout
log4j.appender.BAR.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.BAR.File = bar.log

Does anyone know how I can write the log messages to both log files (as it was doing before I started setting the additivity property) and still prevent the duplicate log messages?

Please note, this is a simplified summary of the problem. In the real world scenario there are multiple loggers and more than two log files


Solution 1:

This problem can be solved in two parts.

1. Prevent duplicate log messages

The log messages were written twice because we listed the FOO appender in both the rootLogger and the log4j.logger.foobar category. So we must remove the appender and only define the logging level in category:

log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO

This means that the INFO level messages from log4j.logger.foobar are propagated upwards to ALL of the loggers the appenders in rootLogger, but will only be written to each log file once.

2. Prevent INFO level message being written to bar.log

Since all of the INFO level log messages for the log4j.logger.foobar category are being inherited by the appenders in rootLogger, we need to stop the BAR appender for recording the INFO level messages.

We can achieve this by setting the Threshold property on the BAR appender itself:

log4j.appender.BAR.Threshold = WARN

This will prevent the INFO level statements being logged in the bar.log file as it will only accept levels of WARN and upwards.

So the complete log4j properties file would be as follows:

log4j.rootLogger = WARN, FOO, BAR
log4j.logger.foobar = INFO

log4j.appender.FOO = org.apache.log4j.RollingFileAppender
log4j.appender.FOO.layout = org.apache.log4j.PatternLayout
log4j.appender.FOO.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.FOO.File = foo.log
log4j.appender.FOO.Threshold = INFO

log4j.appender.BAR = org.apache.log4j.RollingFileAppender
log4j.appender.BAR.layout = org.apache.log4j.PatternLayout
log4j.appender.BAR.layout.ConversionPattern = %d{ISO8601} %-5p %c ~ %m%n
log4j.appender.BAR.File = bar.log
log4j.appender.BAR.Threshold = WARN