Monday, July 16, 2012

Configuration Log4j in Java application, Log4j – log4j.properties examples, RollingFileAppender

Configuration Log4j in Java application, Log4j – log4j.properties examples




log4j.properties
======================================================

# Root logger option
log4j.rootLogger=DEBUG, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\testLogging\\test.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n



=============================================================
RollingFileAppenderExample.java

/**
 *
 */
package com.logging;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * @
 * @see http://logging.apache.org/log4j/1.2/manual.html
 * @author Laxman Gautam
 *
 */
public class RollingFileAppenderExample {

    /**
     * @param args
     */
   
    static Logger logger=Logger.getLogger(RollingFileAppenderExample.class);

    /**
     * @param args
     */
    public static void main(String[] args) {

        PropertyConfigurator.configure("log4j.properties");
        logger.info("HI all ");
        logger.warn("From Laxman ");
        logger.error("This is error logging ");
        logger.fatal("Hope this is useful");
        logger.debug("Bye all ....");


}
}


log output :


21:37:54,634  INFO RollingFileAppenderExample:29 - HI all
21:37:54,638  WARN RollingFileAppenderExample:30 - From Laxman
21:37:54,638 ERROR RollingFileAppenderExample:31 - This is error logging
21:37:54,639 FATAL RollingFileAppenderExample:32 - Hope this is useful
21:37:54,639 DEBUG RollingFileAppenderExample:33 - Bye all ....













No comments:

Post a Comment