Thursday, May 31, 2012

Log4j with multiple appenders using properties.


Please follow the following program to understand log4j implementation for multiple appenders using properties file.

Java : MultipleAppenders
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class MultipleAppenders {

      private static final Logger logger = Logger.getLogger(MultipleAppenders.class);

      public static void main(String[] args) {
            PropertyConfigurator.configure("log.properties");
            logger.debug("Debug Message.");// Method to Method.
            logger.info("Info Message.");// Information passing and recieving.
            logger.warn("Warning Message.");// Retrying again.
      }
}
Properties: log.properties
log4j.rootLogger=DEBUG, Console, File, Html

#Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %p %c --> %M - %m%n
log4j.appender.Console.Threshold = DEBUG

#File Appender
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.File=file.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d %p %c --> %M - %m%n
log4j.appender.File.Threshold = WARN

#File Appender with HTMLLayout
log4j.appender.Html=org.apache.log4j.FileAppender
log4j.appender.Html.File=file.html
log4j.appender.Html.layout=org.apache.log4j.HTMLLayout
log4j.appender.Html.Threshold = TRACE

No comments:

Post a Comment