在 java.util.logging logging.properties 文件中,"handlers" 和“.handlers”有什么区别?

In a java.util.logging logging.properties file, what's the difference between "handlers" and ".handlers"?

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

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

A property ".handlers". This defines a whitespace or comma separated list of class names for handlers classes to load and register as handlers to the specified logger. 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.

由于根记录器的名称是空字符串 (""),在我看来,下面的两个子句应该是等价的:

handlers = myHandler
.handlers = myHandler

这是来自 JDK 的 lib/logging.properties 文件的示例:

handlers= java.util.logging.ConsoleHandler
.level= INFO

我注意到当我尝试枚举根记录器上的处理程序时发生了奇怪的事情。我怀疑这与引用其中一个属性的 LogManager 的实现有关。但是,我想尝试了解我的等价假设是否正确。

澄清一下:我提出这个问题的目的是了解行为是否应该相同。

查看 LogManager 的源代码,似乎对“.handlers”和 "handlers" 这两个属性的处理略有不同。

属性 "handlers" 如果存在,会导致根记录器的处理程序 initialized 在任何其他记录器的处理程序之前。如果使用“.handlers”代替,这个初始化将会发生,但比日志管理器期望的要晚。

您看到的 "weird things" 可能与此延迟初始化处理程序有关。

之所以有两种不同的方法,是因为 New Features in J2SE 5.0. In J2SE 1.4, you could only add handlers to the root logger 使用了 handlers 属性。

根据 Java 1.4 LogManager Java文档:

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.

正如您从您发布的 Java 文档中看到的那样,文档已修改,删除了关于延迟创建的部分。

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

在大多数情况下 handlers.handlers 是等价的,除了:

  1. 使用 handlers 只会在 LogRecord 发布到根记录器处理程序时加载您的处理程序。如果您从不记录任何内容,则永远不会加载处理程序。
  2. 使用 .handlers 将在创建根记录器期间附加您的处理程序。
  3. 如果您同时使用两者,则使用规则 #1 和 #2 将两个属性的联合应用于根记录器。

LogManager 的子类,如 org.apache.juli.ClassLoaderLogManager used in 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.