Exclude a class from a log4j appender

让人想犯罪 __ 提交于 2020-01-01 09:19:17

问题


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



来源:https://stackoverflow.com/questions/4140095/exclude-a-class-from-a-log4j-appender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!