Home > Articles > Programming > Java

Creating JDK 1.4 Logging Handlers

  • Print
  • + Share This
Because of the flexibility they provide, the logging features of JDK 1.4 have become very popular. Jeff Heaton shows you how to create your own handlers to easily extend the logging API to your specific needs.
From the author of

The logging API that is included in JDK 1.4 is intended to be very flexible. This allows you to easily extend the logging API without having to completely replace it. One of the most common ways to extend the logging API is by writing your own handler. In this article, I will show you how to create a handler that allows you to change the logging style of the JDK logging API.

A handler does not just allow you to change the format that the log information is written to. A handler also allows you to specify where the logging information is stored. For example, you could easily create a handler that writes the logging information to a JDBC data source, rather than to the files that logs are generally written to.

Understanding the Structure of the Logging API

Before I show you how to create your own custom handler, I will review the basic structure of the logging API. I will begin by showing you a simple program that makes use of the logging API. TO start with, any class in your program that is going to make use of the logging API must import it. This is done with the following line:

import java.util.logging.*;

This import gives you access to all of the classes in the logging API. To begin using the logging API, your program first must create a new Logger object. The Logger class is provided to you by the logging API. This is done by the following lines of code:

// Create a new logger object
Logger logger = Logger.getLogger(

This creates a new logger object that will log under the name of "com.heaton.articles.logger". This is just a hierarchical name that specifies the log's name; every log will have a name similar to this one. The hierarchical nature allows applications to see the logs from several different applications. For example, the log name "com.heaton" would specify every log that began with the levels "com" and "heaton". The setLevel command specifies that we want to record all levels of severity.

Now that the Logger object has been created, you should write some data to it. The following lines of code show you how a typical log is written to. As you can see, the logging API can accept logged data in a variety of formats:

// try some logging

logger.info("This is how you write a regular entry");
logger.warning("This is how you write a warning");

try {
 int i=0/0;
 } catch ( Exception e ) {
 logger.log(Level.WARNING, "This logs an exception:", e);

The purpose of this article is to show you how to create your own log handler. Notice that this code does not specify a handler to use. By default, the logging API will write the logged information to the console if no additional handlers are specified. To specify an alternate handler to use, the following code should be used:

logger.addHandler( myhandler );
  • + Share This
  • 🔖 Save To Your Account