Log4J is the defacto standard logging framework for Java. Other popular options include Java Logging and Jakarta Commons Logging.

Installing top

Log4J can be downloaded from here. Unzip the downloaded file. To use it, insure that log4j-{version}.jar, found in the dist/lib directory, is in the classpath.

Usage top

The basic steps to use Log4J are:

  1. Configure Log4j.
  2. In each Java class that needs logging,
    1. Obtain a Logger object.
    2. Invoking logging methods on it.

Configuration top

Log4J can be configured in three ways.

  1. from Java code
  2. from a Java property file
  3. from an XML file
The last option is typically preferred.

Here's an example of an XML configuration file, named log4j.xml. Log4J will look for this file in the current directory by default. JEE servers differ in where they look for this. JBoss looks in $JBOSS_HOME/server/default/conf.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Order of child elements is appender*, logger*, root?. --> <!-- Appenders control how logging is output. --> <appender name="file" class="org.apache.log4j.FileAppender"> <param name="File" value="demo.log"/> <layout class="org.apache.log4j.PatternLayout"> <!-- {fully-qualified-class-name}:{method-name}:{line-number} - {message}{newline} --> <param name="ConversionPattern" value="%C:%M:%L - %m%n"/> </layout> </appender> <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%C:%M:%L - %m%n"/> </layout> </appender> <!-- Logger hierarchy example: root - com - com.ociweb - com.ociweb.demo - com.ociweb.demo.LogJDemo --> <!-- Setting additivity to false prevents ancestor categories for being used in addition to this one. --> <category name="com.ociweb" additivity="false"> <priority value="debug"/> <appender-ref ref="file"/> </category> <!-- Levels from lowest to highest are trace, debug, info, warn, error, fatal & off. --> <!-- The root category is used for all loggers unless a more specific logger matches. --> <root> <priority value="error"/> <appender-ref ref="stdout"/> </root> </log4j:configuration>

The DTD referenced in the DOCTYPE of this XML can be found in src/java/org/apache/log4j/xml/log4j.dtd. Copy this to the directory where log4j.xml resides.

To troubleshoot problems with Log4J, it can be useful to add the "debug" attribute to the root element as follows. This will produce additional output when Log4J is configured.

<log4j:configuration debug="true" ...

Example top

Here's an example of using Log4J in Java code. Note how a Logger instance is obtained.

package com.ociweb.demo; import org.apache.log4j.Logger; public class Log4JDemo { private Logger logger = Logger.getLogger(getClass()); public static void main(String[] args) { Log4JDemo app = new Log4JDemo(); // To do something only if debugging is enabled, // such as construct a complicated log message ... //if (logger.isDebugEnabled()) { ... } app.doSomething(); } public Log4JDemo() { logger.debug("message 1"); } public void doSomething() { logger.debug("message 2"); } }

In this example, using the configuration shown earlier, the file demo.log is created in the current directory with the following contents. Note how the method name reported for logging from a constructor is <init>.

com.ociweb.demo.Log4JDemo:<init>:21 - message 1 com.ociweb.demo.Log4JDemo:doSomething:25 - message 2

Methods in the Logger interface for writing log messages include the following. Each of these has two forms. The first takes an Object whose toString value is used for the message. The second takes that plus a Throwable whose stack trace is output after the message. Neither form has a return value.

Methods in the Logger interface for obtaining information about the current level of the Logger include the following. These are commonly used to avoid construction of a complicated log message when it will not be output.

Copyright © 2007 Object Computing, Inc. All rights reserved.