Java Logging Frameworks
Encyclopedia
A Java logging framework is a computer data logging
Computer data logging
Computer data logging is the process of recording events, with an automated computer program, in a certain scope in order to provide an audit trail that can be used to understand the activity of the system and to diagnose problems....

 package for the Java platform.

In software, logging refers to the recording of activity. Logging is a common issue for development teams. Several frameworks ease and standardize the process of logging for the Java platform. This article covers general purpose logging frameworks.

Functionality overview

Logging is broken into three major pieces: the Logger, Formatter and the Handler (Appender). The Logger is responsible for capturing the message to be logged along with certain metadata and passing it to the logging framework. After receiving the message, the framework calls the Formatter with the message. The Formatter accepts the message object and formats it for output. The framework then hands the formatted message to the appropriate Appender for disposition. This might include a console display, writing to disk, appending to a database, or email.

Simpler logging frameworks, like Java Logging Framework by the Object Guy, combine the logger and the appender. This simplifies default operation, but it is less configurable, especially if the project is moved across environments.

Logger

A Logger is an object that allows the application to log without regard to where the output is sent/stored. The application logs a message by passing an object or an object and an exception
Exception
Exception may refer to:* An action that is not part of ordinary operations or standards* Exception handling, in programming languages** or a programming interrupt itself of which exception handling is meant to deal with....

 with an optional severity level to the logger object under a given a name/identifier.

Name

A logger has a name. The name is usually structured hierarchically, with periods (.) separating the levels. A common scheme is to use the name of the class or package that is doing the logging. Both log4j
Log4j
Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java Logging Frameworks....

 and the Java logging API support 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

Severity level

The message is logged at a certain level. Common levels are from Apache Commons Logging:
Common levels
Level Description
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, near 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. Mostly this consists of taking the binary object and converting it to a string representation.

Appenders or handlers

Appenders listen for messages at or above a specified minimum severity level. The Appender takes the message it is passed and posts it appropriately. Message dispositions include:
  • display on the console
  • write to a file or syslog
  • append to a database table
  • distribute via Java Messaging Services
  • send via email
  • write to a socket
  • discard to the "bit-bucket" (/dev/null)

Feature comparison

Features
Framework Supported log levels Standard appenders Popularity Cost / licence
Log4J
Log4j
Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java Logging Frameworks....

FATAL ERROR WARN INFO DEBUG TRACE AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, WriterAppender Widely used in many projects and platforms Apache License, Version 2.0
Java Logging API SEVERE WARNING INFO CONFIG FINE FINER FINEST Sun's default Java Virtual Machine (JVM) has the following: ConsoleHandler, FileHandler, SocketHandler, MemoryHandler Comes with the JRE
Apache Commons Logging FATAL ERROR WARN INFO DEBUG TRACE Depends on the underlying framework Widely used, in conjunction with log4j Apache License, Version 2.0
SLF4J
SLF4J
Simple Logging Facade for Java provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at deployment time and may be , log4j or logback....

ERROR WARN INFO DEBUG TRACE Depends on the underlying framework, which is pluggable MIT License
MIT License
The MIT License is a free software license originating at the Massachusetts Institute of Technology . It is a permissive license, meaning that it permits reuse within proprietary software provided all copies of the licensed software include a copy of the MIT License terms...


Summary

Apache Commons Logging isn't really a logging framework, but a wrapper for one. As such, it requires a logging framework underneath it. It is particularly useful when developing reusable libraries which need to write to whichever underlying logging system is being used by the application. It also provides flexibility in heterogeneous environments where the logging framework is likely to change, although in most cases, once a logging framework has been chosen, there is little need to change it over the life of the project.

The Java Logging API is also not a logging framework, but a standard API for accessing a logging framework. Compatible frameworks can be loaded into JVM and accessed via the API. There is also a logging implementation supplied with the Sun JVM which is the default logging framework accessed by the API. Many developers confuse this implementation with the Java Logging API.

SLF4J
SLF4J
Simple Logging Facade for Java provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at deployment time and may be , log4j or logback....

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

See also

  • SLF4J
    SLF4J
    Simple Logging Facade for Java provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at deployment time and may be , log4j or logback....

  • Log4j
    Log4j
    Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java Logging Frameworks....

  • Javolution
    Javolution
    Javolution is a real-time library aiming to make Java applications faster and more time predictable. Indeed, time-predictability can easily be ruined by the use of the standard library Javolution is a real-time library aiming to make Java applications faster and more time predictable. Indeed,...

     LogContext based on context programming (actual logging framework selectable at run-time).
  • Runtime Intelligence
    Runtime intelligence
    -Introduction:The term runtime intelligence refers to technologies, managed services and practices for the collection, integration, analysis, and presentation of application usage levels, patterns and practices...


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK