Uri Uri - 7 months ago 23
Java Question

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

In the documentation of LogManager, the following is set of the Handlers property:


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.


Since the name of the root logger is the empty string (""), it seems to me like both of the clauses below should be equivalent:

handlers = myHandler
.handlers = myHandler


Here's an example from the JDK's lib/logging.properties file:

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


I've noticed that weird things are happening when I attempt to enumerate the handlers on the root logger. I suspect that this has to do with the implementation of LogManager referring to one of those properties. However, I'd like to try and understand whether I am correct in my assumption of equivalency.

To clarify: My goal with this question is to understand whether the behaviour should be identical.

Answer

The reason there are two different ways is due to New Features in J2SE 5.0. In J2SE 1.4, you could only add handlers to the root logger using the handlers property.

Per the 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.

As you can see from the JavaDocs you posted the documentation was revised removing the part about created lazily.

In Java 5, JDK-4635817 : Attaching handlers to loggers using logging config file was fixed by allowing the use of .handlers Due to backward compatibility, the LogManager had to support both the old and the new ways of installing handlers.

For the most part handlers and .handlers are equivalent except:

  1. Using handlers will only load your handler when a LogRecord is published to the root logger handlers. If you never log anything the handler is never loaded.
  2. Using .handlers will attach your handler during creation of the root logger.
  3. If you use both together then the union of both properties are applied to the root logger using rules #1 and #2.

Subclasses of LogManager like org.apache.juli.ClassLoaderLogManager used in Tomcat apply different rules from what is listed above.