log4j

Log4j is a framework for logging application messages in Java. Within many open source and commercial software products it has developed over the years into a de facto standard. log4j is a pioneer for other logging frameworks, in other programming languages ​​.

History

The project was founded by Ceki Gülcü in 1996 while working at the IBM research laboratory in Zurich. Today it is a part of the logging project of the Apache Software Foundation and is released under the Apache License 2.0. It came at a time when there were no logging mechanisms in the Java standard libraries. Nowadays, it is by its sophistication and configurability for many software developers, the logging system of choice.

The broadcast of the log4j concepts to other programming languages ​​or platforms is so great that there are now a number of adaptations. Some are maintained by the Apache Logging project itself. For example:

  • Log4cxx for C to Log4J compatible with configuration files
  • Log4net for. Net
  • Log4php for PHP

Many variants are, however, developed outside of Apache logging:

  • Log4C for C
  • Log4cplus for C
  • Log4cpp for C
  • Log4Qt for Qt
  • Log4js for JavaScript
  • Log4perl for Perl
  • Log4D for Delphi
  • Log4sh for Unix shells
  • Log4plsql for Oracle PL / SQL
  • Log4sas for SAS Institute
  • AndroidLoggingLog4J
  • Logging module for Python

For some time working on log4j 2.0, which is to replace log4j 1.x.

The Apache Logging Project

The Apache logging project seeks to bring together log4j -like systems for various programming languages. So far, the log4j, log4cxx, log4net, log4php and Chainsaw ( a log file viewer and analysis tool ).

In addition, so-called Companions are developed that provide additional functionality for Apache log4j.

Operation

Instead of issuing errors and informational messages to standard output, log4j is used to forward the reports of so-called logger to the selected logging system. In addition to the selection of the logging system of the message is decided at the same time, due to the importance, whether it is passed through at all. The programmer only needs to worry about the importance of the messages when creating the program. The filtering and type of output can be configured at runtime.

Log4j is designed for the highest possible speed, so that the log does not adversely affect system performance. So take the decision whether a message must be issued on an outdated system (AMD Duron with 800 Mhz, JDK 1.3.1 ) only 5 nanoseconds, the output itself - depending on which layout is used - 21-37 microseconds.

Output scope

In the configuration file, the output can be filtered according to the importance of the messages. The issue circumference increases with the assigned level of importance and includes all messages of level itself, as well as all the more urgent levels. The order arises here as follows:

ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF

For the classification of the importance the following guidelines apply:

Appender

Means appender can vary as desired on standard output to a file, the system log is written to a database, or in any other goals or more in the same time.

The following are the main types of appenders:

Additional appenders can be added any time.

Configuration

There are two ways to configure log4j: using a properties or using an XML file. The configuration is separated from the code, which makes it possible to reconfigure the log without modification or restarting the application. Thus it can be operated only with log level FATAL example, an application until an error occurs. From then on the level WARN is set without stopping the application.

The configuration files define by means of the following components, the behavior of log4j:

Another useful feature is the Mapped Diagnostic Context. In this context a variable is assigned a value and in the configuration file can be referenced to it. In this case, each thread has its own context and can log additional information such as the address of the client in a server application.

Example

The following XML configuration configures an application so that FATAL errors from foreign libraries are logged to the console, ERROR error own application additionally be sent by e -mail, in addition, also INFO messages are logged at a specific component and in a particular class even DEBUG messages.

< DOCTYPE log4j: configuration PUBLIC " - / / APACHE / / DTD LOG4J 1.2 / / EN "      " http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd " >    < - Appender for a one-line output to the console ->                < param name = " ConversionPattern "          value = " % d { ABSOLUTE }% 5p % c {1 }: % L - % m% n " />      < / layout>    < / appender >      < - Appender for the same issue via email ->                                              < param name = " ConversionPattern "          value = " % d { ABSOLUTE }% 5p % c {1 }: % L - % m% n " />      < / layout>    < / appender >      < - ERROR logger for all classes of my application ->                      < / logger >      < - INFO logger for a specific component ->            < / logger >      < - DEBUG Logger for a specific class ->            < / logger >      < - FATAL logger for the entire application (including libraries) ->                < - Logged on console - if not otherwise defined in sub- loggers ->         Apache log4j 2.0

Since 2012, the version 2.0 is initially in the alpha, now in beta status available. The framework was written from scratch, even if parts have been taken over by log4j 1.x. The new version features a modern interface, as it is also known by logback. It also supports native slf4j. At the same time weaknesses of logback were analyzed and tries to improve. Thus loses log4j 2.0, for example, no logging events when the system is reconfigured. In addition, a plug-in architecture has been deployed and enables the configuration using JSON.

The project is currently planning occasional maintenance releases of the series 1.x, but will focus more and more on the 2.x series.

Alternatives

  • Java Logging - since Java 1.4 part of the Java class library; similar to the log4j appender less, no pattern layout
  • Apache Commons Logging - interface for any replaceable logging frameworks, including log4j
  • Tinylog - Slim logging framework with only one static logger
527368
de