Java logging frameworks

From Wikipedia, the free encyclopedia

Contents

[edit] Introduction

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.

[edit] 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 logged, 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 an object and formats it for proper logging. The logging framework then hands the formatted message to the appropriate Appender for disposition of the message. This might include displaying on a console, writing to disk, appending to a database, or notification via email.

Simpler logging frameworks, like Java Logging Framework by the Object Guy, combine the logger and the appender together. This makes for simple initial configuration, but less configurable, especially as the project is moved across environments.

[edit] Logger

Most frameworks support 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 an object 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.

[edit] Name

A logger has a name. The name is usually hierarchical, with periods (.) separating the levels. A common naming scheme is to use the name of the class or package that is doing the loggings. Both log4j and the Java API supported defining Handlers higher up the hierarchy.

For example, the logger might be named "com.sun.some.UsefulClass". The handler can be defined for any of the following:

  • com
  • com.sun
  • com.sun.some
  • com.sun.some.UsefulClass

[edit] 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.

[edit] Formatters or Renderers

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

[edit] 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)

[edit] Comparing Features

Feature Log4J Java Logging API Jakarta Commons Logging
Supported log levels FATAL ERROR WARN INFO DEBUG TRACE SEVERE WARNING INFO CONFIG FINE FINER FINEST FATAL ERROR WARN INFO DEBUG TRACE
Standard Appenders AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, WriterAppender ConsoleHandler, FileHandler, SocketHandler, MemoryHandler Depends on the underlying framework
Popularity[citation needed] Widely used in many project and platforms few many, in conjunction with log4j
Cost/Licence Apache License, Version 2.0 Comes with the JRE Apache License, Version 2.0

[edit] Summary

Of the major players, log4j is still the front runner in the Java Logging domain. The log4j project has been around for a long time and has lots of support from the development community. It's simple to implement, yet has powerful tools built in to accomplish most logging tasks. It is also easily extensible to handle proprietary needs.

The newer logging API, which has been included in the JRE since 1.4, incorporates many of the same concepts as log4j. It has loggers and appenders. However, log4j has been much more broadly used and there are many out-of-the-box solutions in log4j that are lacking in the Java Logging API.[citation needed]

The Jakarta Commons Logging isn't really a logging framework, but a logging framework wrapper. As such, it requires a logging framework underneath it. It would be useful in an heterogeneous environment where the logging framework is likely to change. However, in most cases, once a suitable logging framework has been chosen, there is little need to change it over the life of the project.

SLF4J and Logback, both originally written by the same original writer of log4j, are growing potential replacements in particular for log4j and Jakarta Commons Logging.

[edit] Links to Java Logging Projects

See also: