Sunday, 27 March 2011

Log4j


Simple Log4j Configuration

Log4j is a simple and flexible logging framework. In this tutorial you will learn how to configure log4j for your applications. Let's get started first download the latest version of log4j (Download ). I am using log4j version 1.2.15. Add the log4j-1.2.15.jar to the classpath.
Next you need to create an instance of Logger class. You can create one using theLogger.getLogger(HelloWorld.class) method. It takes one argument the fully qualified class name.
Now we need to configure log4j. The simple way to do that is using BasicConfigurator. configure() method. This will log all the messages on the console.
Now everything is ready you can log messages using any of the print statements of theLogger class. In the following code I use the debug() method to display the "HelloWorld!" message.


package opensourzesupport.helloworld;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class HelloWorld {

    static final Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.debug("Hello World!");
    }
}


The following example shows how to use these methods.The other methods available are info(), warn(), error() and fatal(). Each method represents a logger level namely DEBUG, INFO, WARN, ERROR and FATAL.

package opensourzesupport.helloworld;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
/**
 *
 * @author prasobh.k
 */
public class HelloWorld {

    static final Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.debug("Sample debug message");
        logger.info("Sample info message");
        logger.warn("Sample warn message");
        logger.error("Sample error message");
        logger.fatal("Sample fatal message");
    }
}

Here is the output of the above code.

0 [main] DEBUG com.opensourzesupport.helloworld.HelloWorld  - Sample debug message
0 [main] INFO com.opensourzesupport.helloworld.HelloWorld  - Sample info message
0 [main] WARN com.opensourzesupport.helloworld.HelloWorld  - Sample warn message
0 [main] ERROR com.opensourzesupport.helloworld.HelloWorld  - Sample error message
0 [main] FATAL com.opensourzesupport.helloworld.HelloWorld  - Sample fatal message

The output contains the time elapsed from the start of the program in milliseconds, the thread name, the logger level, the class name and the log message

Using Log4j with Web application

put the following file in WEB-INF/classess

1.commons-logging.properties
2.log4j.properties

commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

log4j.properties

## Root logger
log4j.rootLogger=INFO

#log4j.logger.org=INFO,file,console
log4j.logger.com.mypackage=INFO,file
log4j.logger.org.=INFO,file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/batch.log
log4j.appender.file.MaxFileSize=5000KB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=40
## Layout for the file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{DATE}] %5p [%t] %c{2}:%L - %m%n


## ConsoleAapender
log4j.appender.console=org.apache.log4j.ConsoleAppender
## Layout for the console appender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{DATE}] %5p [%t] %c{5}:%L - %m%n

use following 2 jar files
1.commons-logging-1.1.jar
2.log4j-1.2.11.jar





No comments:

Post a Comment