How to avoid writing messages to the root logger i

2019-08-15 13:32发布

问题:

Is some path to write log messages only to the 'child' logger, avoiding root logger? The root logger is using by other components, so there is no ability to decrease it's level or disable appender. thanks

回答1:

Please use Log4j additivity.

Set the additivity property of a Log4j logger to false and then the log messages which are coming to that logger will not be propagated to parent loggers.

Log4j configuration file:

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.