java 从 log4j appender 中排除一个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4140095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:58:42  来源:igfitidea点击:

Exclude a class from a log4j appender

javalog4j

提问by digiarnie

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

我有一个 log4j.properties 文件,看起来像这样:

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.fooI would want the foo-logappender to be used. However, within that package, I want the BarImpllogs to use the bar-logappender but not the foo-logappender. 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?

通常对于与com.foo我的包结构匹配的类,我希望使用foo-logappender。但是,在该包中,我希望BarImpl日志使用bar-log附加程序而不是foo-log附加程序。目前,BarImpl 写入的任何日志都由 foo-log 和 bar-log 处理(如预期)。如何让 foo-log appender 忽略 BarImpl 类?

回答by Jason

From the Log4j documentation:

从 Log4j 文档:

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.

给定记录器的每个启用的日志记录请求都将转发到该记录器中的所有附加程序以及层次结构中更高的附加程序。换句话说,appender 是从 logger 层次结构中附加地继承的。例如,如果将控制台 appender 添加到根记录器,则所有启用的日志记录请求至少会打印在控制台上。如果另外将文件附加程序添加到记录器,例如 C,则启用的 C 和 C 子项的日志记录请求将打印在文件和控制台上。可以通过将 additivity 标志设置为 false 来覆盖此默认行为,以便 appender 累积不再是可加的。

Try experimenting with the following line (or similar):

尝试使用以下行(或类似行)进行试验:

log4j.additivity.com.foo.BarImpl=false

log4j.additivity.com.foo.BarImpl=false