gpt4 book ai didi

java - 在 java.util.logging logging.properties 文件中, "handlers"和 ".handlers"之间有什么区别?

转载 作者:行者123 更新时间:2023-12-03 23:00:43 25 4
gpt4 key购买 nike

在 LogManager 的文档中,以下是 Handlers 属性的设置:

A property "handlers". This defines a whitespace or comma separatedlist of class names for handler classes to load and register ashandlers on the root Logger (the Logger named "").

A property ".handlers". This defines a whitespace or commaseparated list of class names for handlers classes to load andregister as handlers to the specified logger. Each class name must befor a Handler class which has a default constructor. Note that theseHandlers may be created lazily, when they are first used.


由于根记录器的名称是空字符串 (""),在我看来,下面的两个子句应该是等效的:
handlers = myHandler
.handlers = myHandler
这是 JDK 的 lib/logging.properties 文件中的一个示例:
handlers= java.util.logging.ConsoleHandler
.level= INFO
我注意到当我尝试枚举根记录器上的处理程序时发生了奇怪的事情。我怀疑这与引用这些属性之一的 LogManager 的实现有关。但是,我想尝试了解我的等效假设是否正确。
澄清:我对这个问题的目标是了解行为是否应该相同。

最佳答案

之所以有两种不同的方式是由于New Features in J2SE 5.0 .在 J2SE 1.4 中,您只能 add handlers to the root logger使用 handlers属性(property)。

根据 Java 1.4 LogManager JavaDocs:

A property "handlers". This defines a whitespace separated list of class names for handler classes to load and register as handlers on the root Logger (the Logger named ""). Each class name must be for a Handler class which has a default constructor. Note that these Handlers may be created lazily, when they are first used.



正如您从 JavaDocs 中看到的,您发布的文档已被修改,删除了关于惰性创建的部分。

在 Java 5 中, JDK-4635817 : Attaching handlers to loggers using logging config file已通过允许使用 .handlers 修复由于向后兼容,LogManager 必须同时支持安装处理程序的新旧方法。

大多数情况下 handlers.handlers除了:
  • 使用 handlers只会在 LogRecord 发布到根记录器处理程序时加载您的处理程序。如果您从不记录任何内容,则永远不会加载处理程序。
  • 使用 .handlers将在创建根记录器期间附加您的处理程序。
  • 如果同时使用这两个属性,则使用规则 #1 和 #2 将两个属性的并集应用于根记录器。

  • LogManager 的子类,如 org.apache.juli.ClassLoaderLogManager用于 Tomcat apply different rules from what is listed above .

    JDK 9 有一个回归,将在 .handlers 处修复不能正常工作。这是根据以下提交的: JDK-8191033 Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works .

    关于java - 在 java.util.logging logging.properties 文件中, "handlers"和 ".handlers"之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726431/

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