在 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 的实现有关。但是,我想尝试了解我的等效假设是否正确。澄清:我对这个问题的目标是了解行为是否应该相同。
最佳答案
之所以有两种不同的方式是由于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
将在创建根记录器期间附加您的处理程序。 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/