gpt4 book ai didi

java - Log4j:具有特化的多个类别

转载 作者:行者123 更新时间:2023-12-01 15:38:28 25 4
gpt4 key购买 nike

我对log4j的类别有一些疑问。

我有三个类别...

  • 程序
  • Program.BUILD
  • Program.QUERY

当我定义以下 log4j.properties 时:

log4j.logger.program = DEBUG, stdout, file

log4j.logger.program.BUILD = DEBUG, file

在 Java 中我调用:

Logger logger = Logger.getLogger("program.BUILD");

假设 stdout 和 file 分别是控制台和文件的附加程序。

我的问题是,当我指定两个类别时,如图所示,“program.BUILD 日志”将写入控制台和文件。但他仅被指定用于文件附加程序。那么log4j使它成为继承?

我想指定三个类别,但是当指定时,他仅捕获该类别中指定的内容,而不采用通用类别(程序)。但如果未指定,则类别 program.QUERYprogram.BUILD 将选取程序类别,因为它代表未指定的两个类别。

我该怎么做?

最佳答案

是的,Log4j有继承系统。您可以使用“additivity=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.

(参见 http://logging.apache.org/log4j/1.2/manual.html )

关于java - Log4j:具有特化的多个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8478335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com