Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j : Multiple loggers, levels and appenders

Tags:

log4j

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

like image 795
My Head Hurts Avatar asked Nov 29 '12 13:11

My Head Hurts


People also ask

What are Appenders and loggers in Log4j?

Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.

What is the default logging level in Log4j?

Note that by default Log4j assigns the root logger to Level. ERROR.

How can logger be used in multiple classes?

Use @Log4j annotation with all the classes in which you wish using logger. Benefits: Easy to maintain, easy to track down. Only one object of the logger is created which will be used through out.


1 Answers

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 
like image 181
My Head Hurts Avatar answered Oct 04 '22 18:10

My Head Hurts