Skip to main content

Log4j: Quickly integrate with Java project

This is a quick guide on how to use log4j in your Java project. This task requires only five simple steps listed below. Each of these step is explained in details below.

  1. Set up the project with log4j
  2. Create file
  3. Write a class to record log messages
  4. Compile and run program
  5. Check the log messages

1. Set up the project with log4j

As the first step, let's download the log4j 1.2.17 latest archive (tar or zip). When you extract the archive, log4j-1.2.17.jar file is available in the root of the directory.

Let's create a new project named say log4j-helloworld and copy the log4j-1.2.17.jar into a folder named lib inside the project.

2. Create file

Next task is to create a file (shown below) inside the project folder. This file is the configuration file that provides how, where, which etc instructions on generating log messages.

# root level configurations

# configuration for console outputs

# configuration for file output (into a file named messages.log)

# threshold for file output

3. Write a class to record log messages

Let's write a simple class named Main (in src folder) with a main method to generate some log statements. Logger is added to the class as an attribute and used to write logs of different levels like debug, info and error.

package com.digizol.log4j.helloworld;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class Main {

    private static Logger log = Logger.getLogger(Main.class);

    public static void main(String[] args) {

        log.debug("This is debug method");"This is info method");
        log.error("This is error method");

        log.log(Level.DEBUG, "This is debug from Level.DEBUG");
        log.log(Level.INFO, "This is info from Level.INFO");
        log.log(Level.ERROR, "This is error from Level.ERROR");


4. Compile and run program

If you compile and run from command line, followings are the commands. Make sure to create classes folder inside project for generated class files before executing the commands.
# compile
javac -cp lib/log4j-1.2.17.jar -d classes 
                -sourcepath src src/com/digizol/log4j/helloworld/

# run
java -cp lib/log4j-1.2.17.jar:./classes:./ 

If you are using an IDE like Eclipse, you can simply add log4j jar file into the build path before compiling. When running the Main class, you need to give the path to file in run configuration.

5. Check the log messages

In the Main class, we have added two debug level messages. However the rootLogger is configured in INFO level in file, so none of the debug level log messages are expected as outputs.

Console output as as below.
This is info method
This is error method
This is info from Level.INFO
This is error from Level.ERROR
Console has received both INFO and ERROR log messages, but not DEBUG.

However messages.log file has received only the ERROR level log messages as below.

This is error method
This is error from Level.ERROR

This is because the Threshold level of the file logging appender is set to ERROR which overrides the rootLogger configuration.

Hope this helps you to start using log4j in your next project. I have added this project here for your references.


  1. Jar is added to the build path & is also there. Can you help me with this error msg:

    log4j:WARN No appenders could be found for logger (Main).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See for more info.

  2. @1 This is happening because log4j has failed to locate the file. As you may have noticed, the classpath (-cp) is configured to include the current folder (./) in the below command.

    java -cp lib/log4j-1.2.17.jar:./classes:./ com.digizol.log4j.helloworld.Main

    I have placed the file in the current folder.
    Can you verify that nothing such is missed in your execution?


Post a Comment

We appreciate your opinions, suggestions and criticism.

Popular posts from this blog

Web Services with Apache Axis 1.4 Tutorial: server and client sides

Java Sorting: Comparator vs Comparable Tutorial

Creative Commons License Digizol by Kamal Mettananda is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License .
URL of this page must be supplied in attribution
© 2004-2017