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