Tutorials : Looking into the JDK 1.4 Logging API :

Looking into the JDK 1.4 Logging API

by Keld H. Hansen

Introduction

In JDK 1.4, which was made public in February 2002, Sun introduced a logging API. Until then developers could use one of the many open source logging systems, like Log4J, or implement their own logging mechanism. You'll find many fine articles on the web describing the new Sun Logging API--see the Resources section at the end of my article--so I'll only give you the most important details as a start. We'll then dig down into a few topics which I haven't seen described yet on the web.

But first:

Why is logging important?

The answer is really quite simple: it's a way for your application to tell IT people (developers, administrators, supporters, maintainers, the performance staff, etc.) what's going on inside the Java code. The developers need information during development and debugging. The support staff needs data when the application hits an error. Production people need performance data in order to make best use of computer resources and improve performance. Here are some of the things that could be logged:

  • initialization and shutdown of the application
  • every time a new user logs on and off to the system
  • every time a database report is requested from the system (report name, parameters, cpu usage, )
  • response times for critical requests
  • entry and exit for key modules
  • system errors (it's part of the application's exception handling)
  • every activation of key functions in the application (e.g. from a menu)
  • every call to external systems
  • data that can tell how the application is being used

For most applications you don't have to worry about any negative performance impact caused by the logging code. In production you'd typically switch most logging off, and the JDK 1.4 logging API--and most other popular logging APIs--are designed to be cheap if you don't send much data to the logs. The day you need more logging data--e.g. when a bug has appeared or when you need to measure performance--you can switch logging on for the modules you need.

I hope this'll motivate you for our journey into the design and usage of the JDK 1.4 Logging API.

Loggers, Handlers and Formatters

When using the JDK 1.4 Logging API in your applications you work with Logger objects when sending a message to a log. It's wonderfully simple to work with a logger:

// Define the logger
    Logger logger = Logger.getLogger("hansen.Game");
    . . .
    // Use the logger
    logger.info("Game started");

Couldn't be much simpler.

But where does the message end? This is where the Handlers come into the picture. A handler defines the target for logging. It could be the console, a file, or the Windows NT event log, to name a few possibilities. So every logger needs at least one handler in order for the messages to have a place to go. The JDK defines 5 handlers:

  • StreamHandler : sends messages to an OutputStream
  • ConsoleHandler: sends messages to System.err
  • FileHandler: sends messages to a file
  • SocketHandler: sends messages to a TCP port
  • MemoryHandler: buffers messages in memory

The ConsoleHandler and the FileHandler are probably the most popular ones, covering most applications basic needs.

Finally we have the Formatters. As their name imply they determine the layout of the messages. Sun has defined two formatters:

  • SimpleFormatter: defines a simple message format containing among other things a timestamp
  • XMLFormatter: a message in XML format

Every handler has an associated formatter.

In this example we have defined two loggers. Logger1 has a single ConsoleHandler that uses the SimpleFormatter. Logger2 also has a ConsoleHandler (maybe with other properties) but also has a FileHandler that writes messages in XML format.

It's possible to define your own handlers and formatters.

The default set-up which you get if you don't do anything special is a ConsoleHandler using the SimpleFormatter. If you place the code fragment found at the beginning of this article in a main method and run it, you'll get this written to System.err:

11-08-2002 18:57:37 hansen.playground.logging.LogDef main
    INFO: Game started 
   

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.