java - log4j 将所有日志输出定向到标准输出,即使它不应该

标签 java log4j

在我的 log4j.properties 中我有:

log4j.rootLogger=DEBUG,stdout

log4j.logger.notRootLogger=DEBUG,somewhereelse

appender stdout 和 somewhereelse 都配置正确,stdout 写入控制台,somewhereelse 写入文件。

在每个类的代码中,我要么设置:

static Logger log =  Logger.getLogger("notRootLogger);

^ 当我不想让内容进入控制台时。

-或-

static Logger log = Logger.getRootLogger();

^ 当我这样做的时候。

我必须在 log4.properties 中做什么才能阻止写入 notRootLogger 的内容最终出现在 stdout 中?根记录器写入的任何地方是否存在某种需要以某种方式关闭的继承?

我不想为我只想记录到控制台的每个类单独配置记录器。

最佳答案

您需要设置 additivity = false,IIRC。来自log4j manual :

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.

试试这个:

log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false

关于java - log4j 将所有日志输出定向到标准输出,即使它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1244147/

相关文章:

java - 从文件读取到数组列表

java - 如何使用两个 Multi-Tenancy 数据源之间的事务复制数据?

java - Spring ExceptionHandler 日志记录不起作用

java - Log4j 记录器的生命周期及其使用的资源

java - 减少 SOAPUI API 日志记录

java - Swing 中的 JFormattedTextField 或 JTextField 中仅允许整数和小数值

java - 判断链表是否是回文

java - 使用 Maven 在 JRE 6 下编译执行

java - Log4j2:如何指定要保留的最大副本数?

java - log4j FileAppender 如何被弃用