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