log4j
This article needs additional citations for verification. (May 2009) |
Developer(s) | Apache Software Foundation |
---|---|
Stable release | 1.2.16
/ April 6, 2010 |
Repository | |
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[1] 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 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 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
<?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>
<!-- the root category -->
<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[2]. 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. |
%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.
Log4j in application servers
Apache Tomcat
log4j can be activated in many application servers, including Tomcat, however an extra jar 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 | 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 |
Splunk | Free Lite Version | Splunk can index and analyze Apache log files; it is a commercial product, but also offers a free version. |
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 [citation needed] log file viewer based on Eclipse. |
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-like formatting and has additional performance features. Released under Apache License and hosted by Google Code. Last version is r28, released in September 2010. [3]
- Log4cxx - Apache port for C++. Last version is 0.10.0, released in April 2008 [4]
- Log4cplus - Port for C++, hosted by SourceForge. Last version is 1.0.4, released in October 2010 [5]
- Log4cpp - Yet another port for C++, hosted by SourceForge. Last version is 0.3.4, released in 2002 [6]
- dlib::logger - Another open source port for C++
- Log4plsql HomePage - A port to the Oracle PL/SQL
- Log4c - A port for C. Log4C is a C-based logging library, released on SourceForge under the LGPL license. For various Unix operating systems the autoconf and automake files are provided. On Windows a Makefile is provided for use with MSVC. Developers 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 configuration file. Last version is 1.2.1, released in 2007, and the project is no longer actively developed [7].
- log4perl - A port for Perl. Last version 1.31, released in October 2010. [8]
- log4js - A port for JavaScript. Log4js is available under the licence of Apache Software Foundation. One special feature of Log4js is the ability to log the events of the browser remote on the server. Using Ajax it is possible to send the logging events in several formats (XML, JSON, plain ASCII 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 [9].
- log4javascript - Another port for JavaScript. log4javascript is a JavaScript logging framework based on the log4j. The latest version is 1.4.1, released in March 2009. [10]
- logging - The official logging module for Python inspired by log4j, but not a port of it.
- Log4r - Ruby logging module inspired by log4j
- Apache Log4net - A port to the Microsoft .NET Framework. The initial work was done by Neoworks and was donated to the Apache Software Foundation 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.10, released in 2006[11].
- log4php a port for PHP
- logback - a purported successor to log4j, designed by log4j's founder. Last version is 0.9.26, released in October, 2010 [12]
- log4sh a port for various Unix shells including sh, bash, dash and ksh
- Log4Cocoa - A Port for Cocoa (hosted by SourceForge). Last version is beta3, released in July 2009. [13]
- Log4D - A Port for Delphi (hosted by SourceForge). Last version is 0.9, released in 2007 [14]
- Microlog - A logging framework for Java ME 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.
- Log5F - A port for ActionScript 3.0
- log4eclipse plug-in for Eclipse
Bibliography
- Gülcü, Ceki (February, 2010), The Complete Log4j Manual (2nd ed.), QOS.ch, p. 204, ISBN 978-2970036906
{{citation}}
: Check date values in:|date=
(help) - Gupta, Samudra (June 22, 2005), Pro Apache Log4j (2nd ed.), Apress, p. 224, ISBN 978-1590594995