从log4j附加程序中排除类 [英] Exclude a class from a log4j appender
问题描述
我有一个log4j.properties文件,看起来像这样:
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
通常,对于与com.foo
的包结构匹配的类,我希望使用foo-log
附加程序.但是,在该程序包中,我希望BarImpl
日志使用bar-log
附加程序,而不要使用foo-log
附加程序.目前,由BarImpl编写的任何日志均由foo-log和bar-log处理(如预期的那样).如何获取foo-log附加程序来忽略BarImpl类?
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?
推荐答案
来自Log4j文档:
针对给定记录器的每个启用的日志记录请求都将转发给该记录器中的所有附加程序以及层次结构中较高的附加程序.换句话说,追加器是从记录器层次结构中继承而来的.例如,如果将控制台附加程序添加到根记录器,则所有已启用的日志记录请求将至少在控制台上打印.如果另外将文件追加器添加到记录器(例如C),则对C和C的子级启用的日志记录请求将打印在文件和控制台上.通过将可加性标志设置为false,可以覆盖此默认行为,以便不再增加附加器累积量.
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
log4j.additivity.com.foo.BarImpl=false
这篇关于从log4j附加程序中排除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!