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?
{ 8 comments }
Thanks a lot Veera.
U made a quite difficult things to understand with simplicity.
Thanks for too simple yet power concept. !!
you are welcome!
Thanks a lot Veera.
U made a quite difficult things to understand with simplicity.
you are welcome.
Simple and informative. Thanks
Thanks Veera, for the simple and yet clear info.
Thanks Veera Your tutorial is too good and easy to digest!! You’re great!!!
Comments on this entry are closed.