从log4j应用器中排除一个类[英] Exclude a class from a log4j appender

本文是小编为大家收集整理的关于从log4j应用器中排除一个类的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

通常,对于匹配com.foo软件包结构的类,我希望使用foo-log appender.但是,在该软件包中,我希望BarImpl日志使用bar-log appender,而不是foo-log appender.目前,Barimpl编写的任何日志都由Foo-Log和Bar-Log(如预期)处理.我如何让foo-log appender忽略barimpl类?

推荐答案

来自log4j文档:

每个启用给定记录器的记录请求将转发给该记录器中的所有附录以及层次结构中较高的附录.换句话说,附录是从记录器层次结构中添加的.例如,如果将控制台Appender添加到Root Logger中,则所有启用的记录请求至少将在控制台上打印.如果此外,如果将文件appender添加到记录器中,例如C,则启用了C和C子女的记录请求,将在文件和控制台上打印.可以覆盖此默认行为,以使Appender积累不再是通过将添加性标记设置为False.

.

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

log4j.additivity.com.foo.barimpl = false

本文地址:https://www.itbaoku.cn/post/1574896.html

问题描述

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?

推荐答案

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