Datasheet
Creating Your Own Filter
An example of a custom filter is a filter that discards any log message that does not start with “client”.
This is useful if log messages are coming from a number of sources, and each log message from a partic-
ular client (or clients) is prefixed with the string
“client”:
import java.util.logging.*;
public class ClientFilter implements java.util.logging.Filter {
public boolean isLoggable(LogRecord record)
{
if(record.getMessage().startsWith(“client”))
return(true);
else
return(false);
}
}
The ErrorManager
The ErrorManager is associated with a handler and is used to handle any errors that occur, such as
exceptions that are thrown. The client of the logger most likely does not care or cannot handle errors, so
using an
ErrorManager is a flexible and straightforward way for a Handler to report error conditions.
The error manager defines a single method:
void error(String msg, Exception ex, int code)
This method takes the error message (a string), the Exception thrown, and a code representing what
error occurred. The codes are defined as static integers in the
ErrorManager class and are listed in the
following table.
Error Code Description
CLOSE_FAILURE Used when close() fails
FLUSH_FAILURE Used when flush() fails
FORMAT_FAILURE Used when formatting fails for any reason
GENERIC_FAILURE Used for any other error that other error codes don’t match
OPEN_FAILURE Used when open of an output source fails
WRITE_FAILURE Used when writing to the output source fails
Logging Examples
By default, log messages are passed up the hierarchy to each parent. Following is a small program that
uses a named logger to log a message using the
XMLFormatter:
import java.util.logging.*;
public class LoggingExample1 {
public static void main(String args[])
56
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 56