A Java Log4J example

Java Log4J FAQ: Can you share a simple Log4J example?

I don't have a lot of time today to get into all the Log4J properties and configuration options you can use, but I wanted to share at least one Java Log4J example here, so here goes.

A Log4J example Java class

The following Java Log4J example class is a simple example that initializes, and then uses, the Log4J logging library for Java applications. As you can see the configuration is pretty simple.

package com.devdaily.log4jdemo;

import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

 * A simple Java Log4j example class.
 * @author alvin alexander, devdaily.com
public class Log4JExample
  // our log4j category reference
  static final Category log = Category.getInstance(Log4JDemo.class);
  static final String LOG_PROPERTIES_FILE = "lib/Log4J.properties";

  public static void main(String[] args)
    // call our constructor
    new Log4JExample();

    // Log4J is now loaded; try it
    log.info("leaving the main method of Log4JDemo");

  public Log4JExample()
    log.info( "Log4JExample - leaving the constructor ..." );

  private void initializeLogger()
    Properties logProperties = new Properties();

      // load our log4j properties / configuration file
      logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
      log.info("Logging initialized.");
    catch(IOException e)
      throw new RuntimeException("Unable to load logging property " + LOG_PROPERTIES_FILE);

Java Log4J example class - discussion

As you can see in this Java Log4J example class, after a few class level fields are created, the action in our example begins with the main method, which first calls the class constructor. The constructor then calls the initializeLogger method. This method actually does the work of loading the Log4J properties (configuration) file. It then calls the configure method of the PropertyConfigurator class.

Once this is done I call the info method of the Log4j log object several times. Notice that I could have also called other methods like logger.warn(), log.debug(), log.error(), or log.fatal(), but to keep it simple I'm just showing log.info().

Our Java Log4J properties file

A Log4J example class like this may not make much sense unless you also see the corresponding Log4J properties file, so ... I also need to show the Log4J configuration file that I've used with my Java class.

My Log4J properties file is in the slightly older Log4j properties file format, because I actually prefer this format to the newer XML property file format. My Log4J properties file is named Log4J.properties, and for the purpose of this demonstration I'm keeping it in a sub-directory of my project named lib. Here are the contents of this Log4J properties file:

# our log4j properties / configuration file
# STDOUT appender
log4j.appender.STDOUT.layout.ConversionPattern=%d %p [%t] %C{1} - %m\n

# use the STDOUT appender. set the level to INFO.
log4j.category.com.devdaily.log4jdemo.Log4JDemo=INFO, STDOUT

More Java Log4J examples - what's related

I hope this simple Java Log4J example has been helpful. As I wrap up my current collection of Log4J examples, here's a link to my other Log4J tutorials: