Tuesday, April 3, 2012

Log4j In Action

Logging is very important for the maintenance of  a deployed application. Just assume a situation that we deploy our application on the client systems and work on another project. After some time client reports a failure of our system. What will happen if we haven't maintained a log file! We may be in a big trouble in such situations. It may be very hard to track where the bug occurred. Because we haven't any IDE's , debuggers on deployed machine.

So it is always recommended to use a logging mechanism for large applications. Logging may be a overhead if we don't use it carefully. We can use logging inside exception handling blocks.


Log4j is a simple and solid logging framework. Lets see how we can use Log4j in our projects. There are 5 main logging levels.
  • INFORMATION
  • DEBUG
  • WARN
  • ERROR
  • FATAL
Log4j can be configured in 3 ways.
  • Basic configuration
  • Property file configuration
  • XML configuration.
Most popular way is using property file configuration. We will see how to use basic config. and property file config.

Example using Basic configuration.
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class Log4jSimpleConfigDemo {
  static final Logger logger = Logger.getLogger(Log4jSimpleConfigDemo.class);

  public static void main(String args[]) {
    BasicConfigurator.configure();

    logger.info("This is an INFORMATION level message");
    logger.debug("This is a DEBUG level message");
    logger.warn("This is a WARN level message");
    logger.error("This is an ERROR level message");
    logger.fatal("This is a fatal level message");

  }
}

Output
1 [main] INFO Log4jSimpleConfigDemo  - This is an INFORMATION level message
2 [main] DEBUG Log4jSimpleConfigDemo  - This is a DEBUG level message
3 [main] WARN Log4jSimpleConfigDemo  - This is a WARN level message
3 [main] ERROR Log4jSimpleConfigDemo  - This is an ERROR level message
3 [main] FATAL Log4jSimpleConfigDemo  - This is a fatal level message

Eample using property file.
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jPropertyFileConfigDemo {
  static final Logger logger = Logger.getLogger(Log4jPropertyFileConfigDemo.class);

  public static void main(String[] args) {
    PropertyConfigurator.configure("log4j.properties");

    logger.info("This is an INFORMATION level message");
    logger.debug("This is a DEBUG level message");
    logger.warn("This is a WARN level message");
    logger.error("This is an ERROR level message");
    logger.fatal("This is a fatal level message");

  }

}

Property file (log4j.properties)
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Logging into the console is not always appropriate. Lets see how we can can logging into a file.
This configuration makes no changes to the source code but for the property file.

Property file (log4j.property)
log4j.rootLogger=DEBUG, CA, FA
 
#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=mylog.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
 
# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = WARN




0 comments:

Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis