Log4j
Encyclopedia
Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü
Ceki Gülcü
Ceki Gülcü is the founder of the log4j project and the author of The complete log4j manual. He has since started the SLF4J and Logback projects, with the intention of offering a compatible successor to log4j....

 and is now a project of the Apache Software Foundation
Apache Software Foundation
The Apache Software Foundation is a non-profit corporation to support Apache software projects, including the Apache HTTP Server. The ASF was formed from the Apache Group and incorporated in Delaware, U.S., in June 1999.The Apache Software Foundation is a decentralized community of developers...

. log4j is one of several Java Logging Frameworks
Java Logging Frameworks
A Java logging framework is a computer data logging 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...

.

Gülcü has since started the 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 projects, with the intention of offering a successor to log4j.

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 More detailed information. Expect these to be written to logs only. Since version 1.2.12.

Configuration

There are two ways to configure log4j. One is with a properties file and the other is with an XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

 file. Within either you can define 3 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, 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
Internet socket
In computer networking, an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet....

 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 (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 / C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 function printf
Printf
Printf format string refers to a control parameter used by a class of functions typically associated with some types of programming languages. The format string specifies a method for rendering an arbitrary number of varied data type parameter into a string...

. There are also HTMLLayout and XMLLayout formatters for use when HTML
HTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....

 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








value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />

























TTCC

TTCC is a message format used by log4j. TTCC is 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.
%X{key} Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event for specified key
%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.

Apache Tomcat

log4j can be activated in many application servers, including Tomcat, however an extra jar
JAR (file format)
In software, JAR is an archive file format typically used to aggregate many Java class files and associated metadata and resources into one file to distribute application software or libraries on the Java platform.JAR files are built on the ZIP file format and have the .jar file extension...

 library must be added. Other application servers are available and may have log4j built in.

There are separate instructions on how to use log4j with Tomcat

Log viewers

Log4J log file contain semi structured information about application problems, in some cases you would run log analysis on the logs in order to detect bugs, errors and generate statistics.
Name Free Description
Pattern Viewer Free Trial. Handle any file format. Search/Sort/Filter/Compare.
Chainsaw
Chainsaw (log file viewer)
Chainsaw is a java-based GUI tool to view and analyze log files - specifically logs generated by the Log4j logging system. Both Log4j and Chainsaw are Open source projects under Apache Software Foundation. The latest release is Chainsaw v2...

Yes Chainsaw is a java-based GUI viewer. Chainsaw uses similar configuration file as log4j. It is also an Apache project.
log2web Yes This is open source and web-based, but has fewer features than Chainsaw
StackHub Unknown StackHub is a web-based service which can consume Log4J events and alert on errors and user-defined thresholds.
Log4View Paid for and free. Log4View is a .NET based viewer.
SawMill Paid for. Web-Based
XpoLog Paid for and Free Web-Based
Logview4j Free Logview4j is open-source but has only tcp-socket server feature.
LogMX Paid for and Free LogMX is an extendable cross-platform log viewer aims to be user-friendly, and to have powerful filtering capabilities.
LogSaw Yes LogSaw is a high-performance log file viewer based on Eclipse
Eclipse (software)
Eclipse is a multi-language software development environment comprising an integrated development environment and an extensible plug-in system...

.
logFaces Commercial logFaces is a centralized logging server with a real-time log viewer.
Log Viewer Commercial a commercial, log4j log viewer and log monitor
XLog-Solution Free and Commercial Log aggregation from distributed application. Web-based dashboard with control over filters. Email alerts. Interface with Jira.
OtrosLogViewer Free OtrosLogViewer load logs from remote servers using ftp, sftp, ssh, samba. Have powerful log filtering and highlighting capabilities.

Ports

  • log5j - Positioned as a 'modern' facade to log4j that supports printf
    Printf
    Printf format string refers to a control parameter used by a class of functions typically associated with some types of programming languages. The format string specifies a method for rendering an arbitrary number of varied data type parameter into a string...

    -like formatting and has additional performance features. Released under Apache License
    Apache License
    The Apache License is a copyfree free software license authored by the Apache Software Foundation . The Apache License requires preservation of the copyright notice and disclaimer....

     and hosted by Google Code
    Google Code
    Google Code is Google's site for developer tools, APIs and technical resources. The site contains documentation on using Google developer tools and APIs—including discussion groups and blogs for developers using Google's developer products....

    . Last version is r28, released in September 2010.
  • Log4cxx - Apache port for C++. Last version is 0.10.0, released in April 2008
  • Log4cplus - a port for C++, hosted by SourceForge
    SourceForge
    SourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...

    . Last version is 1.0.4, released in January 2011
  • Log4cpp - Yet another port for C++, hosted by SourceForge
    SourceForge
    SourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...

    . Last version is 0.3.4, released in 2002
  • dlib::logger - Another open source port for C++
  • Log4plsql HomePage - A port to the Oracle PL/SQL
    PL/SQL
    PL/SQL is Oracle Corporation's procedural extension language for SQL and the Oracle relational database...

  • Log4c - A port for C. Log4C is a C-based 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....

     library, released on SourceForge
    SourceForge
    SourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...

     under the LGPL license. For various Unix
    Unix
    Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

     operating systems the autoconf and automake files are provided. On Windows
    Microsoft Windows
    Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

     a Makefile is provided for use with MSVC. Developers
    Software developer
    A software developer is a person concerned with facets of the software development process. Their work includes researching, designing, developing, and testing software. A software developer may take part in design, computer programming, or software project management...

     may also choose to use their own make system to compile the source, depending on their build engineering requirements. An instance of the log4c library may be configured via three methods: using environment variables, programmatically, or via XML
    XML
    Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

     configuration file. Last version is 1.2.1, released in 2007, and the project is no longer actively developed.
  • log4perl - A port for Perl
    Perl
    Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

    . Last version 1.32, released in February 2011.
  • log4js - A port for JavaScript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

    . Log4js is available under the licence of Apache Software Foundation
    Apache Software Foundation
    The Apache Software Foundation is a non-profit corporation to support Apache software projects, including the Apache HTTP Server. The ASF was formed from the Apache Group and incorporated in Delaware, U.S., in June 1999.The Apache Software Foundation is a decentralized community of developers...

    . One special feature of Log4js is the ability to log the events of the browser remote on the server. Using Ajax
    Ajax (programming)
    Ajax is a group of interrelated web development methods used on the client-side to create asynchronous web applications...

     it is possible to send the logging events in several formats (XML
    XML
    Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

    , JSON
    JSON
    JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

    , plain ASCII
    ASCII
    The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

    , etc.) to the server to be evaluated there. The following appenders are implemented for log4js: AjaxAppender, ConsoleAppender, FileAppender, JSConsoleAppender, MetatagAppender, and WindowsEventsAppender. The following Layout classes are provided: BasicLayout, HtmlLayout, JSONLayout, and XMLLayout. Last version is 1.1, released in 2008.
  • log4javascript - Another port for JavaScript. log4javascript is a JavaScript logging framework based on the log4j. The latest version is 1.4.2, released in October 2011.
  • logging - The official logging module for Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

     inspired by log4j, but not a port of it.
  • logging - R
    R (programming language)
    R is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....

     logging module inspired by log4j
  • Log4r - Ruby
    Ruby (programming language)
    Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

     logging module inspired by log4j
  • Log4erl - A logger for Erlang in the spirit of log4j
  • Apache Log4net - A port to the Microsoft .NET Framework
    .NET Framework
    The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

    . The initial work was done by Neoworks and was donated to the Apache Software Foundation
    Apache Software Foundation
    The Apache Software Foundation is a non-profit corporation to support Apache software projects, including the Apache HTTP Server. The ASF was formed from the Apache Group and incorporated in Delaware, U.S., in June 1999.The Apache Software Foundation is a decentralized community of developers...

     in February 2004. The framework is similar to the original log4j while taking advantage of new features in the .NET runtime. Provides Nested Diagnostic Context (NDC) and Mapped Diagnostic Context (MDC). Last version is 1.2.11, released in 2011.
  • log4php a port for PHP
    PHP
    PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

  • logback - a purported successor to log4j, designed by log4j's founder. Last version is 0.9.29, released in June, 2011
  • log4sh a port for various Unix shells including sh, bash, dash and ksh
  • Log4Cocoa - A port for Cocoa
    Cocoa (API)
    Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...

     (originally hosted by SourceForge
    SourceForge
    SourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...

    ) (Latest updates available on GitHub).
  • Log4D - A port for Delphi
    Borland Delphi
    Embarcadero Delphi is an integrated development environment for console, desktop graphical, web, and mobile applications.Delphi's compilers use its own Object Pascal dialect of Pascal and generate native code for 32- and 64-bit Windows operating systems, as well as 32-bit Mac OS X and iOS...

     (hosted by SourceForge
    SourceForge
    SourceForge Enterprise Edition is a collaborative revision control and software development management system. It provides a front-end to a range of software development lifecycle services and integrates with a number of free software / open source software applications .While originally itself...

    ). Last version is 0.9, released in 2007
  • Microlog - A logging framework for Java ME
    Java Platform, Micro Edition
    Java Platform, Micro Edition, or Java ME, is a Java platform designed for embedded systems . Target devices range from industrial controls to mobile phones and set-top boxes...

     inspired by Log4j.
  • Microlog4Android - A logging framework for Android inspired by Log4j, i.e. with the Log4j API as opposed to the built-in logging in Android.
  • AndroidLoggingLog4J - Provides logging with Log4J in Android additionally providing a LogCat appender.
  • Log5F - A port for ActionScript
    ActionScript
    ActionScript is an object-oriented language originally developed by Macromedia Inc. . It is a dialect of ECMAScript , and is used primarily for the development of websites and software targeting the Adobe Flash Player platform, used on Web pages in the form of...

     3.0
  • log4eclipse plug-in for Eclipse
  • Log4qt - Qt port.

External links


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....

  • Chainsaw
    Chainsaw (log file viewer)
    Chainsaw is a java-based GUI tool to view and analyze log files - specifically logs generated by the Log4j logging system. Both Log4j and Chainsaw are Open source projects under Apache Software Foundation. The latest release is Chainsaw v2...

  • logback a purported successor to log4j, designed by log4j's founder
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK