Creating a Handler Class
Now, you will be shown how to create a handler class. Any handler class must subclass the Handler class provided by the logging API. The class Handler is declared abstract. To create a functional Handler class, you must override the following three abstract methods: close, flush, and publish. By implementing these three methods, you will be passed all of the logging information that your handler is expected to log. The signatures for these three methods are shown here:
abstract public void close(); abstract public void flush(); abstract public void publish(LogRecord record);
Obviously, these are not the only methods that are allowed in your handler. You may add any other methods or properties that are needed to properly implement your handler. One such method is the constructor. You can use the constructor to accept information about where your handler will be storing its data. One example would be a handler that logged to a JDBC data source. You could use the constructor to receive information about the database connection.
To properly implement a handler, you must implement the close method, which should close whatever medium was opened in the constructor. This is important so that system resources are properly closed. This is particularly important when files are used, so that the files are properly closed and do not lose any data.
In addition to the close method, you must implement a flush method. The flush method is called to ensure that all data up to this point has been written to whatever medium the handler is storing it to. For example, if you were logging to a file, you would simply call the flush method of the output stream that you were logging to. The flush method does not always make sense to implement.
Often, you may find that you have no need of a flush method. This is particularly true if you have implemented your handler so that the data logged is immediately written. If data is immediately written, there are no temporary buffers to flush. Regardless if you immediately write your data or not, you still must implement a flush method. If you are not going to use the flush method, simply create an empty method.
Finally, we must create the publish method, which is the one that does most of the work. The publish method is called for each logging event. The publish method accepts a single parameter, of type LogRecord. The LogRecord object contains all of the information that is to be logged for this event. A publish method will generally call all of the "get" methods contained in the LogRecord and write the data to whatever medium this log handler is using.
The LogRecord object passed to a publish method contains a variety of information. A level is used to indicate how severe the log event was. This level allows certain parts of the program to filter out unimportant log events. The LogRecord also contains the source class and method to allow you to trace what part of the program generated the log event. The log event also contains the actual text of the log entry. There are also other, less-frequently used pieces of information that are also passed with the LogRecord.