log4j

Apache log4j
Developer(s) Apache Software Foundation
Initial release January 8, 2001 (2001-01-08)[1]
Stable release 2.2 / February 22, 2015 (2015-02-22)
Written in Java
Operating system Cross-platform
Type Logging Tool
License Apache License 2.0
Website http://logging.apache.org/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.

Gülcü has since started the SLF4J and Logback[2] projects, with the intention of offering a successor to log4j.

The log4j team has created a successor to log4j with version number 2.0.[3] log4j 2.0 was developed with a focus on the problems of log4j 1.2, 1.3, java.util.logging and logback, and addresses issues which appeared in those frameworks. In addition, log4j 2.0 offers a plugin architecture which makes it more extensible than its predecessor. log4j 2.0 is not backwards compatible with 1.x versions,[4] although an "adapter" is available.

Log4j 1 Log level

The following table defines the log levels and messages in log4j, in decreasing order of severity. The left column lists the log level designation in log4j and the right column provides a brief description of each log level.

Level Description
OFF The highest possible rank and is intended to turn off 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.
WARN 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 Most detailed information. Expect these to be written to logs only. Since version 1.2.12.[5]

Configuration of log4j 1.2

There are three ways to configure log4j: with a properties file, with an XML file and through Java code. Within either you can define three main components: Loggers, Appenders and Layouts. Configuring logging via a file has the advantage of turning logging on or off without modifying the application that uses log4j. The application can be allowed to run with logging off until there's a problem, for example, and then logging can be turned back on simply by modifying the configuration file.

Loggers are logical log file names. They are the names that are known to the Java application. Each logger is independently configurable as to what level of logging (FATAL, ERROR, etc.) it currently logs. In early versions of log4j, these were called category and priority, but now they're called logger and level, respectively.

The actual outputs are done by Appenders. There are numerous Appenders available, with descriptive names, such as FileAppender, DailyRollingFileAppender, ConsoleAppender, SocketAppender, SyslogAppender, NTEventLogAppender and even SMTPAppender. Multiple Appenders can be attached to any Logger, so it's possible to log the same information to multiple outputs; for example to a file locally and to a socket listener on another computer.

Appenders use Layouts to format log entries. A popular way to format one-line-at-a-time log files is PatternLayout, which uses a pattern string, much like the C / C++ function printf. There are also HTMLLayout and XMLLayout formatters for use when HTML or XML formats are more convenient, respectively.

To debug a misbehaving configuration use the Java VM property -Dlog4j.debug which will output to standard out. To find out where a log4j.properties was loaded from inspect getClass().getResource("/log4j.properties") or getClass().getResource("/log4j.xml").

There is also an implicit "unconfigured" configuration of log4j, that of a log4j-instrumented Java application which lacks any log4j configuration. This prints to stdout a warning that the program is unconfigured, and the URL to the log4j web site where details on the warning and configuration may be found. As well as printing this warning, an unconfigured log4j application does not print messages at INFO, DEBUG or TRACE levels, and possibly not higher level messages.

Example for log4j 1.2

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <!-- 
         an appender is an output destination, such as the console or a file;
         names of appenders are arbitrarily chosen.
    -->
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
        </layout>
    </appender>
 
    <!-- 
         loggers of category 'org.springframework' will only log messages of level "info" or higher;
         if you retrieve Loggers by using the class name (e.g. Logger.getLogger(AClass.class))
         and if AClass is part of the org.springframework package, it will belong to this category
    -->
    <logger name="org.springframework">
        <level value="info"/>
    </logger>

    <!-- 
         everything of spring was set to "info" but for class 
         PropertyEditorRegistrySupport we want "debug" logging 
    -->
    <logger name="org.springframework.beans.PropertyEditorRegistrySupport">
        <level value="debug"/>
    </logger>
 
    <logger name="org.acegisecurity">
        <level value="info"/>
    </logger>
    
    
    <root>
        <!-- 
            all log messages of level "debug" or higher will be logged, unless defined otherwise 
            all log messages will be logged to the appender "stdout", unless defined otherwise 
        -->
        <level value="debug" />
        <appender-ref ref="stdout" />
    </root>
</log4j:configuration>

TTCC

TTCC is a message format used by log4j.[6] TTCC is an acronym for Time Thread Category Component. It uses the following pattern:

 %r [%t] %-5p %c %x - %m%n

Where

Mnemonic Description
%r Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
%t Used to output the name of the thread that generated the logging event.
%p Used to output the priority of the logging event.
%c Used to output the category of the logging event.
%x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.[7]
%X{key} Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event for specified key.[8]
%m Used to output the application supplied message associated with the logging event.
%n Used to output the platform-specific newline character or characters.

Example output
467 [main] INFO org.apache.log4j.examples.Sort - Exiting main method.

Ports

Apache Log4j 2

Apache Log4j 2 is the successor of Log4j 1 which was released as GA version in July 2014. The framework was rewritten from scratch and has been inspired by existing logging solutions, including Log4j 1 and JUL. The main differences[18] to Log4j 1 are:

One of the most recognized features of Log4j 2 is the performance of the "Asynchronous Loggers".[19] Log4j 2 makes use of the LMAX Disruptor.[20] The library reduces the need for kernel logging and increases the logging performance by the factor 12. For example, in the same environment Log4j 2 can write more than 18,000,000 messages per second, whereas other frameworks like Logback and Log4j 1 just write < 2,000,000 messages per second.

Log4j 2 provides support for SLF4J, Commons Logging, Apache Flume and Log4j 1.

See also

References

  1. "Apache log4j 1.2 Release History". apache.org. Apache Software Foundation. Retrieved 2014-09-02.
  2. "Logback Home". Logback.qos.ch. Retrieved 2014-07-24.
  3. "Log4j 2 Guide - Apache Log4j 2". Logging.apache.org. 2014-07-12. Retrieved 2014-07-24.
  4. "Log4j 2 Guide - Apache Log4j 2: News". Logging.apache.org. 2014-07-12. Retrieved 2014-07-24.
  5. "Level (Apache Log4j 1.2.17 API)". Logging.apache.org. 2012-06-09. Retrieved 2014-07-24.
  6. "TTCCLayout (Apache Log4j 1.2.17 API)". Logging.apache.org. 2012-06-09. Retrieved 2014-07-24.
  7. "Class NDC". Archived from the original on 2007-08-20. Retrieved 2014-07-24.
  8. "MDC (Apache Log4j 1.2.17 API)". Logging.apache.org. 2012-06-09. Retrieved 2014-07-24.
  9. "Logging Framework for C | Free System Administration software downloads at". Sourceforge.net. Retrieved 2014-07-24.
  10. berliOS | Fraunhofer Institut FOKUS. "berliOS | berliOS Suche". Log4js.berlios.de. Retrieved 2014-07-24.
  11. "a JavaScript logging framework". log4javascript. Retrieved 2014-07-24.
  12. "Logging JavaScript errors to your server side log". JSNLog. Retrieved 2014-07-24.
  13. "Apache log4net: Home". Logging.apache.org. 2015-12-05. Retrieved 2016-04-08.
  14. "log4perl - log4j for Perl". Mschilli.github.com. Retrieved 2014-07-24.
  15. "Apache Logging Services". Apache.org. Retrieved 2015-03-11.
  16. "tmuth/Logger-A-PL-SQL-Logging-Utility ¡ GitHub". Github.com. Retrieved 2014-07-24.
  17. "Log4db2 by angoca". Angoca.github.io. Retrieved 2014-07-24.
  18. "The new log4j 2.0". Grobmeier.de. 2012-12-05. Retrieved 2014-07-24.
  19. "Log4j 2 Asynchronous Loggers for Low-Latency Logging - Apache Log4j 2". Logging.apache.org. 2014-07-12. Retrieved 2014-07-24.
  20. "Disruptor by LMAX-Exchange". Lmax-exchange.github.io. Retrieved 2014-07-24.

Further reading

  • Gülcü, Ceki (February 2010), The Complete Log4j Manual (2nd ed.), QOS.ch, p. 204, ISBN 978-2-9700369-0-6 
  • Gupta, Samudra (June 22, 2005), Pro Apache Log4j (2nd ed.), Apress, p. 224, ISBN 978-1-59059-499-5 

External links

This article is issued from Wikipedia - version of the Friday, April 08, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.