Java Reference
In-Depth Information
figure 6-17  
using a logging api
An alternative approach to debugging is using a logger to create log messages about the execution of your
program. There is a built‐in logging API in the java.util.logging package, which you will see here,
but there are also several popular external APIs built for logging that you can try on your own. Logging
allows you to check what's happening while you're developing, but it also offers a way to continue to
monitor a program that's in use. You can configure your logger to log on different levels, to output to
places other than the console, and to customize the output to whatever is most useful for the application.
There are eight predefined logging levels: Off, Finest, Finer, Fine, Config, Info, Warning, and Severe. As
the name suggests, the Off level indicates no logging at all. The Finest level is designed for development
and testing and logs the most messages. This is not appropriate for live applications, as the speed slows
considerably with this amount of logging. As you move up through the levels, fewer and fewer messages
are logged. The Finer and Fine levels offer less detailed messages. Info logs messages that give informa-
tion about what is happening in the program, including connections and messages. The Warning level
logs messages that indicate problems, while the Severe level only logs those most concerning problems
that indicate a failure. The benefit of all these levels is that you can set up logging and then adjust the
level depending on the types of messages you need or want to see, without making any changes to your
program. The levels are organized numerically and messages are similarly scored numerically. In this
way, a message with an importance value of 3 will be logged in any level 3 or higher.
There are two main components to a logging system: a logger and a handler. The logger picks up
messages and checks the level of the message against the logging level in place. If the message seems
to fit the level (or above), the logger will use a filter to determine for sure if the message should be
logged. If it passes this more strict filter, the message is sent to the handler. The handler may also
use a filter to check the message and a formatter to format the record as it should be written.
The process of logging is designed to offer flexibility, and for that reason, there are a lot of pos-
sibilities in how you set up and use your logger. This section only begins to scratch the surface, so
that you can add a basic logger to your program and access the log messages. To begin logging,
you should create a logger instance, using the getLogger() method, in each class you wish to
 
Search WWH ::




Custom Search