We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 29 August 2013

Log4j – Multiple appender using Property configuration


In this tutorials we are going to see how to create simple log4j application with multiple appenders using property configuration.I used console appender and file appender in this example, you can use any number of appenders as your wish.
Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.multipleappenders under log4j example,add log4j jar in your build path.I used log4j-1.2.17.jar jar for this application.you can download this jar from Apache web site.Create log4j.properties in your project directory or source directory, add the following configuration
log4j.properties
#The root logger 
logFile = C:/log
log4j.rootLogger = DEBUG, FILE,console

#The console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender

#The layout for console appender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%r [%t] %p %c %x - %m%n


#The file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${logFile}/log.log

#The layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%r [%t] %p %c %x - %m%n

rootLogger used to configure Level and Appender, in this example I used DEBUG as Level and Console appender,File Appenders are used as appender(console,FILE are name of appender).

PatternLayout is used to configure log4j message format.

Now create java class called Log4jMultipleAppenders under com.javatutorialscorner.log4j.multipleappenders package.

Log4jMultipleAppenders.java

package com.javatutorialscorner.log4j.multipleappenders;

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

public class Log4jMultipleAppenders {

private static final String FILE_PATH = "log4j.properties";
private static final Logger log = Logger
.getLogger(Log4jMultipleAppenders.class);

/**
* @param args
*/
public static void main(String[] args) {
if (FILE_PATH == null) {
BasicConfigurator.configure();
log.info("Log4j Configured using Basic Configurator");
} else {
PropertyConfigurator.configure(FILE_PATH);
log.info("Log4j Configured using Property Configurator");
}

log.debug("Debug message by Java Tutorials Corner");
log.info("Info message by Java Tutorials Corner");
log.warn("Warn message by Java Tutorials Corner");
log.error("Error message by Java Tutorials Corner");
log.fatal("Fatal message by Java Tutorials Corner");

}

}

Create instance for Logger class , getLogger(Log4jMultipleAppenders.class) method accept fully qualified class name as its argument. In this example if property file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  PropertyConfigurator.configure(FILE_PATH)  loads the configuration from property file .

now you can run the application, you will see the following output in console and also in file at specified location. 

0 [main] INFO com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Log4j Configured using Property Configurator

16 [main] DEBUG com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Debug message by Java Tutorials Corner


16 [main] INFO com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Info message by Java Tutorials Corner


16 [main] WARN com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Warn message by Java Tutorials Corner


16 [main] ERROR com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Error message by Java Tutorials Corner


16 [main] FATAL com.javatutorialscorner.log4j.multipleappenders.Log4jMultipleAppenders  - Fatal message by Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Log4j – Multiple appender using Property configuration Rating: 5 Reviewed By: eHowToNow