Java Reference
In-Depth Information
Obviously this problem has cropped up many times, and there is a common solution: use
a logging framework. Doing so ensures that we can
Send logging messages to a desired location (file/screen/printer/nowhere)
Log messages even if the application is running in “server” mode and does not have a
window for messages to appear in
Ensure that logging is done in a consistent manner throughout the entire application
Turn logging on and off selectively throughout the application at deploy time
JDK 1.4 and later provide a standard API for logging. We recommend that you use this API
throughout your application whenever you consider sending something to the standard or
error output.
To use the logger, you must first get an instance of a logger you can use. Loggers are
normally named (although there is an anonymous logger that everyone can use), allowing
you to configure different loggers in different ways. To get a reference to the logger for the
example.com.testApplication context, you would create code similar to this:
Logger myLogger = Logger.getLogger("example.com.testApplication");
All classes that “get” a logger using the same name will get the same instance of the logger.
This ensures that the configuration applied to that logger will apply to all instances of that
logger.
Note The name of the logger is part of a hierarchical namespace, meaning that example.com.
testApplication is a child of the namespace example.com . We will comment further on this later in
this section.
You can define the level at which your messages will be logged. There are several prede-
fined levels, and we recommend that you use them rather than define your own. The predefined
levels are shown in Table 2-10 in order from the least amount of logging to the most amount of
logging.
Table 2-10. Predefined Logging Levels
Logging Level
Recommended Usage
Severe
Serious failure
Warning
Potential problem
Info
Informational messages
Config
Configuration messages
Fine
Tracing (debugging) messages
Finer
Fairly detailed tracing messages
Finest
Highly detailed tracing messages
Search WWH ::




Custom Search