Java logging framework

This is an old revision of this page, as edited by M0smith (talk | contribs) at 18:53, 26 April 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Introduction ot Java Logging Frameworks

Logging is a common issue for most development teams. There have been several frameworks developed to ease and standardize the process of logging for the Java platform. This page covers general purpose logging frameworks. There are other logging frameworks, toolkits and libraries like Tracing Class Loader but that is for another topic.

Functionality Overview

Logging a message is broken into three major pieces:the Logger, Formatter and the Appender (Handler). The Logger is responsible for capturing the message to be logger, along with certain meta-data like level, and passing that to the logging framework. After receiving the message, the logging framework calls the Formatter on the message. The Formatter accepts and object and formats it for proper logging. The logging framework then hands the formatted message to the appropriate Appender to dispostion of the message. This might include displaying on a console, writing to disk, appending to a database, or notification via email.

Logger

Most frameworks support a the notion of a Logger. A Logger is an object that allows the application to log data without regard to where the data is actually logged. The application logs a message in the form of an object or and object and and exception. When a Logger is created, it is given a name or an identifier. When logging a message, it is logged at a certain level or priority.

Name

Level

The message is logged at a certain level. The common levels are, copied from Jakarta Commons Logging:

FATAL Severe errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROR Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARNING Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
INFO Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
DEBUG detailed information on the flow through the system. Expect these to be written to logs only.
TRACE more detailed information. Expect these to be written to logs only.

The logging framework maintains the current logging level for each logger. The logging level can be set more or less restrictive. For example, if the logging level is set to "WARNING", then all messages of that level or higher are logged, ERROR and FATAL.

Formatters or Renderers

A Formatter is an object that formats a given object for logging by the Appender. Mostly this consists of takeing the object and converting it to a string representation.

Appenders or Handlers

The appenders are configured to listen for messages of a certain log level or above. The Appender takes the message it is passed and disposes of the messages. Some message dispositions include:

  • display on the console
  • write to a file or syslog
  • append to a database table
  • distribute via JMS
  • send via email
  • write to a listening socket
  • bit-bucket (/dev/null)

Best Practices

Comparing Features=

Feature Log4J Java Logging API

Summary

See also Open Source Logging Tools in Java