Jump to content

Log4j: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Apache Tomcat: Removing the howto style content.
No edit summary
Line 108: Line 108:
Apache has another project named [[Chainsaw (log file viewer)|Chainsaw]] which was intended to use log4j generated log files. Chainsaw is a java-based GUI viewer with a lot of functionality. Chainsaw also uses similar configuration file as log4j. Other log viewing tools can be used with log4j too, for example [http://log2web.sf.net/ log2web], which is open source and web-based, but has fewer features than Chainsaw. The web-based service [http://stackhub.com/ StackHub] can consume Log4J events and alert on errors and user-defined thresholds. Another .NET based viewer with lots of functionality is [http://www.log4view.com/ Log4View], which is a commercial product but also offers a free Community Edition.
Apache has another project named [[Chainsaw (log file viewer)|Chainsaw]] which was intended to use log4j generated log files. Chainsaw is a java-based GUI viewer with a lot of functionality. Chainsaw also uses similar configuration file as log4j. Other log viewing tools can be used with log4j too, for example [http://log2web.sf.net/ log2web], which is open source and web-based, but has fewer features than Chainsaw. The web-based service [http://stackhub.com/ StackHub] can consume Log4J events and alert on errors and user-defined thresholds. Another .NET based viewer with lots of functionality is [http://www.log4view.com/ Log4View], which is a commercial product but also offers a free Community Edition.
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.
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.

==References==
{{reflist}}

==Bibliography==
{{refbegin}}
*{{citation
| first1 = Rob
| last1 = Harrop
| first2 = Ian
| last2 = Darwin
| date = August 30, 2004
| title = Pro Jakarta Velocity: From Professional to Expert
| edition = 1st
| publisher = [[Apress]]
| pages = 370
| isbn = 978-1590594100
| url = http://www.apress.com/book/view/9781590594100
}}
*{{citation
| first1 = Jim
| last1 = Cole
| first2 = Joseph
| last2 = D. Gradecki
| date = July 16, 2003
| title = Mastering Apache Velocity
| edition = 1st
| publisher = [[John Wiley & Sons|Wiley]]
| pages = 372
| isbn = 978-0471457947
| url = http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471457949.html
}}
{{refend}}


== See also ==
== See also ==
Line 115: Line 148:
*[http://sourceforge.net/projects/log4eclipse log4eclipse]
*[http://sourceforge.net/projects/log4eclipse log4eclipse]
*[http://stackhub.com StackHub]
*[http://stackhub.com StackHub]

==References==
{{reflist}}


==External links==
==External links==

Revision as of 20:01, 4 September 2009

Apache log4j
Developer(s)Apache Software Foundation
Stable release
1.2.15 / September 29, 2007 (2007-09-29)
Repository
Written inJava
Operating systemCross-platform
TypeLogging Tool
LicenseApache License 2.0
Websitehttp://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 compatible 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
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. Was only added in 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 a file locally and to a socket listener on another computer, for example.

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

Example

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC
"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd">
<log4j:configuration>
    <!-- an appender is an output destination, such as e.g. 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 springframework.org 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 do want
debug logging -->
    <logger name="org.springframework.beans.PropertyEditorRegistrySupport">
        <level value="debug"/>
    </logger>
 
    <logger name="org.acegisecurity">
        <level value="info"/>
    </logger>
 
    <root><!-- the root category -->
        <!-- all log messages of level debug or more serious 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>

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 viewer

Apache has another project named Chainsaw which was intended to use log4j generated log files. Chainsaw is a java-based GUI viewer with a lot of functionality. Chainsaw also uses similar configuration file as log4j. Other log viewing tools can be used with log4j too, for example log2web, which is open source and web-based, but has fewer features than Chainsaw. The web-based service StackHub can consume Log4J events and alert on errors and user-defined thresholds. Another .NET based viewer with lots of functionality is Log4View, which is a commercial product but also offers a free Community Edition. 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.

References

Bibliography

  • Harrop, Rob; Darwin, Ian (August 30, 2004), Pro Jakarta Velocity: From Professional to Expert (1st ed.), Apress, p. 370, ISBN 978-1590594100
  • Cole, Jim; D. Gradecki, Joseph (July 16, 2003), Mastering Apache Velocity (1st ed.), Wiley, p. 372, ISBN 978-0471457947

See also

Ports