Datasheet
Stock Handlers
The java.util.logging package includes a number of predefined handlers to write log messages to
common destinations. These classes include the
ConsoleHandler, FileHandler, MemoryHandler,
SocketHandler, and StreamHandler. These classes provide a specific implementation of the abstract
methods in the
Handler class. All the property key names in the tables are prefixed with java.util
.logging
in the actual properties file.
The
StreamHandler serves chiefly as a base class for all handlers that write log messages to some
OutputStream. The subclasses of StreamHandler are ConsoleHandler, FileHandler, and
SocketHandler. A lot of the stream handling code is built into this class. See the following table for a
list of properties for the
StreamHandler.
Property Name Description Default Value
StreamHandler.level Log level for the handler Level.INFO
StreamHandler.filter Filter to use undefined
StreamHandler.formatter Formatter to use java.util.logging
.SimpleFormatter
StreamHandler.encoding
Character set encoding to use Default platform encoding
The following methods are defined/implemented on the
StreamHandler class.
Method Description
void close() The head string from the Formatter will be written if it
hasn’t been already, and the
tail string is written before the
stream is closed.
void flush() Writes any buffered output to the stream (flushes the stream).
boolean isLoggable Performs standard checks against level and filter, but
(LogRecord record) also returns false if no output stream is open or the record
passed in is null.
void publish(LogRecord If the record passed in is loggable, the Formatter is then
record) invoked to format the log message and then the message is
written to the output stream.
void setEncoding(String Sets the character encoding to use for log messages. Pass in
encoding) null to use the current platform’s default character encoding.
protected void Sets an OutputStream to use. If an OutputStream is
setOutputStream already open, it is flushed and then closed. The new
(OutputStream out) OutputStream is then opened.
The
ConsoleHandler writes log messages to System.err. It subclasses StreamHandler but overrides
close() to only perform a flush, so the System.err stream does not get closed. The default formatter
used is
SimpleFormatter. The following table describes the properties that can be defined in the
logging.properties file for the ConsoleHandler.
48
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 48