Log4j Tutorial: Additivity – what and why?

by Veera on August 13, 2009

in Java

In my last post about how to send log messages to different log files,  I’d promised to write about additivity. And, here is a post for keeping my promise. I assume that after reading my previous article, now you know how to configure Log4j for logging into multiple files. But, there is a catch in that technique. To explain this clearly, consider this scenario:

You’ve configured a total of three appenders in your application. One for the package com.demo.moduleone and one for com.demo.moduletwo and one root logger for com.demo. The configuration will look something like this (showing only the appender configuration, excluding other details)

log4j.category.com.demo.moduleone = INFO, moduleOneFileAppender
log4j.category.com.demo.moduletwo = INFO, moduleTwoFileAppender
log4j.rootLogger = INFO, rootFileAppender

In Log4j, all the loggers are following some hierarchies. ie, A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.

So, as per the hierarchy, our rootFileAppender is the parent appender for both moduleOneFileAppender and moduleTwoAppender. So, all the log messages that coming to the child loggers will be propagated to the parent appenders too. So, in our scenario, the log messages from the package com.demo.moduleone will be sent to the moduleOneFileAppender as well as the rootFileAppender. The same applies to the com.demo.moduletwo also. This leads to write the same log message in two different location.

How to avoid this redundancy?

In order to avoid this redundancy, we can use the option additivity. Simply set the additivity for a logger to false and now the log messages which are coming to that logger will not be propagated to it’s parent loggers. So, our new configuration file would be:

log4j.category.com.demo.moduleone = INFO, moduleOneFileAppender
log4j.additivity.com.demo.moduleone = false
log4j.category.com.demo.moduletwo = INFO, moduleTwoFileAppender
log4j.additivity.com.demo.moduletwo = false
log4j.rootLogger = INFO, rootFileAppender

With the above configuration, the log messages from the com.demo.moduleone will go to the moduleOneAppender only and the rest of the log messages will go to the rootFileAppender.

Previous: How to send log messages to different log files?

Follow me on Twitter to get notified whenever I update this blog.

{ 8 comments }

Tanzy September 10, 2009 at 10:53 AM

Thanks a lot Veera.

U made a quite difficult things to understand with simplicity.

Raminder Singh October 19, 2009 at 7:00 PM

Thanks for too simple yet power concept. !!

Veera October 19, 2009 at 9:10 PM

you are welcome!

Tanzy November 13, 2009 at 11:03 PM

Thanks a lot Veera.

U made a quite difficult things to understand with simplicity.

Veera February 4, 2010 at 10:05 AM

you are welcome.

Chenna January 22, 2010 at 2:37 AM

Simple and informative. Thanks

Mubarak February 6, 2010 at 1:52 PM

Thanks Veera, for the simple and yet clear info.

Jisha February 11, 2010 at 8:07 PM

Thanks Veera Your tutorial is too good and easy to digest!! You’re great!!!

Comments on this entry are closed.

Previous post:

Next post: