Wednesday, October 15, 2014

Log4j in 1 minute - Log4j Quick Start Guide

In this tutorial I am showing you the minimum steps you need to use log4j in your project.

Note:-
In this example I am using RollingFileAppender as the appender. RollingFileAppender lets you to write logs in to a log file. The default maximum size of the file is 10MB. You can change this value in log4j.properties file.

1. Create a new java project.
2. Download log4j-2.x.xx.jar from http://logging.apache.org/log4j/2.x/download.html and add it to your project(Im using log4j-1.2.12.jar).
3. Create a new property file in your source folder(src) and name it as log4j.properties. Then add below content to it.

log4j.rootLogger = debug, myFileAppender
log4j.appender.myFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myFileAppender.File=D://log.log
log4j.appender.myFileAppender.layout=org.apache.log4j.PatternLayout

4. Now you can use various logging methods in your classes as below.
import org.apache.log4j.Logger;

public class Log4jExample{

  static Logger log = Logger.getLogger(Log4jExample.class.getName());

  public static void main(String[] args) {
     log.debug("Hello this is an debug message");
     log.info("Hello this is an info message");
     log.warn("Hello this is an warn message");
     log.error("Hello this is an error message");
     log.fatal("Hello this is an fatalmessage");
    }
}

One minute tutorial is over. Now you know how to use log4j.
Happy logging with log4j...!!




Below I will include some extra information.

If you want to change the maximum size of the log file you can do it as below.
log4j.appender.myFileAppender.MaxFileSize=200KB

If you want to bakup the old log files after max file size reached, use MaxBackupIndex property as below. The MaxBackupIndex is the nuber of bakup files you need to keep.
log4j.appender.myFileAppender.MaxBackupIndex=3

If you want to print the log messages in console too, you need to configure a ConsoleAppender too. Then your complete property file would looks like below. Newly added content has been marked with different color.

log4j.rootLogger = debug, myFileAppender, myConsole
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.myFileAppender.MaxFileSize=2GB
log4j.appender.myFileAppender.MaxBackupIndex=3
log4j.appender.myFileAppender.File=D://log.log
log4j.appender.myFileAppender.layout=org.apache.log4j.PatternLayout

Even though you have set the log level of the rootLogger to debug you can set the log level of an appender to a higher level. For an exemple you can set the log level of the appender myFileAppender to Error level like this.
log4j.appender.myFileAppender.Threshold=ERROR

There are lot more properties that you can use to customize the output. Just google and you could find them.

NOTE:In above example we used log4js RootLogger with multiple appenders. If you want you can define your own loggers like this.

log4j.logger.myLogger = info, myFileAppender2

In this case myFileAppender2 prints only the logs which come through myLogger logger. The appenders bound to root logger prints all the logs come through root logger and myLogger.

1 comment:

  1. Many thanks for your effort in this clear 1 minute Log4J quick start Guide!

    ReplyDelete