Exclude a class from a log4j appender

2019-04-06 20:34发布

问题:

I have a log4j.properties file that looks something like this:

log4j.logger.com.foo=INFO, foo-log
log4j.logger.com.foo.BarImpl=INFO, bar-log

Usually for classes that match the package structure of com.foo I would want the foo-log appender to be used. However, within that package, I want the BarImpl logs to use the bar-log appender but not the foo-log appender. At the moment, any logs written by BarImpl is handled by both foo-log and bar-log (as expected). How do I get the foo-log appender to ignore the BarImpl class?

回答1:

From the Log4j documentation:

Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.

Try experimenting with the following line (or similar):

log4j.additivity.com.foo.BarImpl=false



标签: java log4j