Sunday, November 8, 2009

Overview of Log4j with example



Introduction to Log4j

Almost every large application includes its own logging or tracing API. In conformance with this rule, the E.U. SEMPER project decided to write its own tracing API. This was in early 1996. After countless enhancements, several incarnations and much work that API has evolved to become log4j, a popular logging package for Java. The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/ . By the way, log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

Inserting log statements into code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is usually the case for multithreaded applications and distributed applications at large.

A Log4J example class

Here's a quick Log4j tutorial, with a lot of Log4j Java source code.
The following Java class is a very 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.techpage.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.
 */
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()
  {
    initializeLogger();
    log.info( "Log4JExample - leaving the constructor ..." );
  }
 
  private void initializeLogger()
  {
    Properties logProperties = new Properties();
 
    try
    {
      // load our log4j properties / configuration file
      logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
      PropertyConfigurator.configure(logProperties);
      log.info("Logging initialized.");
    }
    catch(IOException e)
    {
      throw new RuntimeException("Unable to load logging property " + LOG_PROPERTIES_FILE);
    }
  }
}


After a few class level fields are created, the action in this Java Log4j example class begins with the main method, which first calls the class constructor. The constructor then calls theinitializeLogger method. This method actually does the work of loading the Log4J properties (configuration) file. It then calls the configure method of the PropertyConfiguratorclass.
Once this is done I call the info method of the Log4j logobject several times. Notice that I could have also called other methods like logger.warn(), log.debug(), log.error(), orlog.fatal(), but to keep it simple I'm just showinglog.info().

The Log4J properties file

Before I leave this quick tip I also need to show the Log4J configuration file that I'm using. My file is in the slightly older Log4j properties file format, because I actually prefer this format to the newer XML property file format. It is namedLog4J.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:


#
# our log4j properties / configuration file
#
# STDOUT appender
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d %p [%t] %C{1} - %m\n
 
# use the STDOUT appender. set the level to INFO.
log4j.category.com.techpage.log4jdemo.Log4JDemo=INFO, STDOUT



Stumble
Delicious
Technorati
Twitter

0 Comments:

Post a Comment