Datasheet
hierarchy. The names of the Logger objects, going down the tree, are dot-separated. Therefore, java
.util
is the parent Logger of java.util.ArrayList. You can name the loggers any arbitrary string,
but keeping with the dot-separated convention helps with clarity.
The simplest use of the logging system creates a
Logger and uses all system defaults (defined in a prop-
erties file) for the logging system. The following example outputs the log message using a formatting
class called the
SimpleFormatter that adds time/date/source information to the log message:
import java.util.logging.*;
public class BasicLoggingExample {
public static void main(String args[])
{
Logger logger = Logger.getLogger(“BasicLoggingExample”);
logger.log(Level.INFO, “Test of logging system”);
}
}
The following is output from the BasicLoggingExample:
Feb 22, 2004 4:07:06 PM BasicLoggingExample main
INFO: Test of logging system
The Log Manager
The entire logging system for a particular application is controlled by a single instance of the LogManager
class. This instance is created during the initialization of the LogManager. The LogManager contains the
hierarchical namespace that has all the named
Logger objects. The LogManager also contains logging con-
trol properties that are used by
Handlers and other objects in the logging system for configuration. These
configuration properties are stored in the file
lib/logging.properties that is located in the JRE installa-
tion path.
There are two system properties that can be used to initialize the logging system with different proper-
ties. The first way is to override the property
java.util.logging.config.file and specify the full
path to your own version of
logging.properties. The other property, java.util.logging.config
.class
, is used to point to your own LogManager. This custom LogManager is responsible for reading
in its configuration. If neither of these properties is set, Java will default to the
logging.properties
file in the JRE directory. Consult the following table for properties that can be set on the LogManager in
this file. You can also specify properties for
Loggers and Handlers in this file. These properties are
described later in this section.
Property Key Property Value
Handlers Comma-separated list of Handler classes. Each handler must be located
somewhere in the system classpath.
.level Sets the minimum level for a specific Logger.
The
level must be prefixed with the full path to a specific Logger. A
period by itself sets the level for the root logger.
36
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 36