September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Logging with log4j-An Efficient Way to Log Java Applications

  • October 22, 2003
  • By Mugdha Vairagade
  • Send Email »
  • More Articles »

Appender

Appender component is interface to the destination of log statements, a repository where the log statements are written/recorded. A logger object receives log request from log statements being executed, enables appropriate ones, and sends their output to the appender(s) assigned to it. The appender writes this output to repository associated with it. There are various appenders available; such as ConsoleAppender(for console), FileAppender (for file), JDBCAppender (for database), SMTPAppender (for SMTP server), SocketAppender (for remote server) and even Instant Messenger (for IMAppender).

An appender is assigned to a logger using the addAppender( ) method of the Logger class, or through external configuration files. A logger can be assigned one or more appenders that can be different from appenders of another logger. This is useful for sending log outputs of different priority levels to different destinations for better monitoring. For example: All log outputs with levels less than FATAL and ERROR being sent to files, while all those with levels equal to ERROR and FATAL sent to console for faster detection.

A logger also implicitly inherits appenders from its parents (and from ancestors, in that effect). Therefore, the log requests accepted by logger are sent to its own appenders along with that of all its ancestors. This phenomenon is known as appender additivity.

Layout

The Layout component defines the format in which the log statements are written to the log destination by appender. Layout is used to specify the style and content of the log output to be recorded; such as inclusion/exclusion of date and time of log output, priority level, info about the logger, line numbers of application code from where log output originated, and so forth. This is accomplished by assigning a layout to the appender concerned.

Layout is an abstract class in log4j API; it can be extended to create user-defined layouts. Some readymade layouts are also available in a log4j package; they are PatternLayout, SimpleLayout, DateLayout, HTMLLayout, and XMLLayout.

Implementing and Configuring log4j

Implementing and configuring log4j is quite easy. The following sections show how to do it.

Requirement

The only requirement for installing and using log4j is the source of log4j API, freely available for download in compressed (tar or zip) files (see Resources).

Installation and running log4j

Download the compressed log4j source, uncompress it, save the resulting log4j-1.2.4.jar at any desired location and include its absolute path in the application's CLASSPATH. Now, log4j API is accessible to user's application classes and can be used for logging.

To log an application class, follow these steps:

  1. Import log4j package in the class.
  2. Inside the class, instantiate a logger object using Logger.getLogger( ) static method.
  3. Instantiate layouts (readymade or user-defined) to be assigned to appenders.
  4. Instantiate appenders and assign desired layout to them by passing the layout object as parameter to their constructors.
  5. Assign the instatiated appenders to the Logger object by invoking its addAppender( ) method with desired appender as parameter.
  6. Invoke appropriate printing methods on Logger object to perform logging.

Steps 3, 4, and 5 can be skipped in case of external configuration.

Listing 2: A class com.foo.sampleapp.MyClass being logged with log4j.

/* Application package */
package com.foo.sampleapp;

/*Import necessary log4j API classes
import org.apache.log4j.*;

public class MyClass {

/* get a static logger instance with name
   com.foo.sampleapp.MyClass   */
static Logger myLogger =
       Logger.getLogger(MyClass.class.getName( ));
Appender myAppender;
SimpleLayout myLayout;


/* Constructor */
public MyClass(){ 
...
/* Set logger priority level programmatically. Though this is
   better done externally  */
myLogger.setLevel(Level.INFO);
...
/* Instantiate a layout and an appender, assign layout to
   appender programmatically */
myLayout = new SimpleLayout();
myAppender = new ConsoleAppender(myLayout);    // Appender is
                                               // Interface
...
/* Assign appender to the logger programmatically */
myLogger.addAppender(myAppender);
...
...
} //end constructor

public void do_something( int a, float b){

/* This log request enabled and log statement logged, since
   INFO = INFO */
myLogger.info("The values of parameters passed to method
               do_something are: " + a, b); */
...
/* this log request is not enabled, since DEBUG < INFO*/
   myLogger.debug("Operation performed successfully");
...
if (x == null){
/* this log request is enabled and log statement logged, since
   ERROR > INFO*/
myLogger.error("Value of X is null");
...
}
}    //end do_something()

}    // end class MyClass

Upon application execution the resulting log output will look like Listing 3:

Listing 3 Log output generated by logging the class MyClass.

INFO - The values of parameters passed to method do_something are:
       21, 34.8f
ERROR - Value of X is null




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel