Log4j is a package to output log statements to a variety of output targets.

The log statements can remain in your shipped code without incurring a heavy performance cost. The logging behavior can be controlled by editing a configuration file (log4j.properties), without modifying the application.

One of the distinctive features of log4j is the notion of inheritance in loggers. By using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity. This helps reduce the volume of logged output and minimize the cost of logging.

The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.

Log4j has three main components (loggers, appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Log4j is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

More information about log4j can be found at http://logging.apache.org/log4j/docs/index.html

The latest Log4j version can be downloaded from http://logging.apache.org/log4j/docs/download.html

rss feed

Installing Log4j 1.2.9.


Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Not applicable

  1. Download and unzip logging-log4j-1.2.9.zip
    For example: c:\tools\logging-log4j-1.2.9

  2. The log4j-1.2.9.jar file which you will later need is located at: C:\Tools\logging-log4j-1.2.9\dist\lib

  3. You can use Log4j in web applications or standalone applications. A comprehensive example in how to setup Log4j in a web application, see quick guide "Setup logging with Log4J using Struts 1.2.4 on Tomcat 4.1.3."

    An example in how to setup Log4j in a standalone application, see the following steps.

  4. Set the LOG4J_HOME system variable to the directory where you installed Log4j.
    e.g.: LOG4J_HOME=C:\Tools\logging-log4j-1.2.9

  5. Edit the CLASSPATH system variable:
    e.g.: CLASSPATH=%CLASSPATH%;%LOG4J_HOME%\dist\lib\log4j-1.2.9.jar

  6. Create the following directories:


  7. Create the file c:\demo\com\mobilefish\Log4jDemo.java.

    package com.mobilefish;
    import org.apache.log4j.Logger;

    public class Log4jDemo {

          static Logger log = Logger.getLogger("com.mobilefish.Log4jDemo");

          public static void main(String args[]) {

                log.debug("This is my debug message.");
                log.info("This is my info message.");
                log.warn("This is my warn message.");
                log.error("This is my error message.");
                log.fatal("This is my fatal message.");

  8. Open a dos window and type:

    cd c:\demo

  9. Compile the java code, type:

    javac ./com/mobilefish/Log4jDemo.java

  10. Create file c:\demo\log4j.properties

    log4j.rootLogger=ERROR, stdout
    log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n

    Note: The log4j.properties file must be put in the directory where you issued the java command.

  11. Run your application:

    java com.mobilefish.Log4jDemo

    The following log messages will be displayed:

    [ERROR] 08:34 (Log4jDemo.java:main:13)
    This is my error message.

    [FATAL] 08:34 (Log4jDemo.java:main:14)
    This is my fatal message.

  12. There are additional parameters you can add to the java command line:

    • To debug Log4j use parameter: -Dlog4j.debug. For example:

      java -Dlog4j.debug=true com.mobilefish.Log4jDemo

      The following messagess will be displayed:

      log4j: Trying to find [log4j.xml] using context
             classloader [email protected]
      log4j: Trying to find [log4j.xml] using
             [email protected] class loader.
      log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
      log4j: Trying to find [log4j.properties] using context classloader
             [email protected]
      log4j: Using URL [file:/C:/demo/log4j.properties] for
             automatic log4j configuration.
      log4j: Reading configuration from URL file:/C:/demo/log4j.properties
      log4j: Parsing for [root] with value=[ERROR, stdout].
      log4j: Level token is [ERROR].
      log4j: Category root set to ERROR
      log4j: Parsing appender named "stdout".
      log4j: Parsing layout options for "stdout".
      log4j: Setting property [conversionPattern] to
             [[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n].
      log4j: End of parsing for "stdout".
      log4j: Parsed "stdout" options.
      log4j: Finished configuring.
      [ERROR] 13:52 (Log4jDemo.java:main:13)
      This is my error message.

      [FATAL] 13:52 (Log4jDemo.java:main:14)
      This is my fatal message.

    • If you want to use another filename instead of log4j.properties: -Dlog4j.configuration. For example:

      Rename log4j.properties to test.properties
      java -Dlog4j.configuration=test.properties com.mobilefish.Log4jDemo