Datasheet
Each library is designed for flexibility of usage. Familiarity with these libraries is vital when developing
solutions in Java. The more tools on your belt as a developer, the better equipped you are.
Java Logging
Java has a well-designed set of classes to control, format, and publish messages through the logging sys-
tem. It is important for a program to log error and status messages. There are many people who can ben-
efit from logging messages, including developers, testers, end users, and people working in the field that
have to troubleshoot programs without source code. It is vital to include a high number of quality log
messages in a program, from status updates to error conditions (such as when certain exceptions are
caught). By using the logging system, it is possible to see what the program is doing without consulting
the source code, and most importantly, track down error conditions to a specific part of the program.
The value of a logging system is obvious, especially in large systems where a casual error with minimal
or no log messages might take days or longer to track down.
The logging system in
java.util.logging is sophisticated, including a way to prioritize log messages
such that only messages a particular logger is interested in get logged, and the messages can be output
to any source that a
Handler object can handle. Examples of logging destinations are files, databases,
and output streams. Take a close look at Figure 1-1 to see an overview of the entire logging system.
Figure 1-1
The specific
Logger objects are actually hierarchical, and though not mandatory, can mirror the class
hierarchy. When a
Logger receives a log message, the message is also passed automatically to the parent
of
Logger. The root logger is named “ “ (the empty string) and has no parent. Each other Logger is
usually named something such as
java.util or java.util.ArrayList to mirror the package/class
Logger
Handler
Formatter
FilterFilter
Filters are used to
determine whether to
process or skip a log
message
Only the last Handler in the
chain of Handlers can
apply a Formatter to the
message
Handler passes message
to next Handler in a chain
of Handlers
Passes log message to
current Logger’s parent
Log message
has an associated
log level. Logger
skips m
essages
below a particular
level
Formatter can
localize/transform
log message
Logging destination
Each Handler knows how
to write a log message to a
particular destination
Client code
35
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 35