728x90 AdSpace

Thursday, 29 August 2013

Log4j – Multiple appenders using XML configuration

In this tutorials we are going to see how to create simple log4j application with multiple appenders using xml 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.xml in your project directory or source directory, add the following configuration
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%r [%t] %p %c %x - %m%n" />
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="file" value="${user.home}/log.log" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%r [%t] %p %c %x - %m%n" />

<level value="DEBUG" />
<appender-ref ref="FILE" />
<appender-ref ref="console" />

We used DOMConfigurator.configure() to load xml configuration.root tag  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).Layout  is used to define log message layout pattern which is configured inside appender tag PatternLayout is used to configure log4j message conversion pattern.

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


package com.javatutorialscorner.log4j.multipleappenders;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jMultipleAppenders {

private static final String FILE_PATH = "log4j.xml";
private static final Logger log = Logger

* @param args
public static void main(String[] args) {
if (FILE_PATH == null) {
log.info("Log4j Configured using Basic Configurator");
} else {
log.info("Log4j Configured using XML 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 xml file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise  DOMConfigurator.configure(FILE_PATH)  loads the configuration from xml 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 XML configurator

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

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

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

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

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

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

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


Post a Comment

Item Reviewed: Log4j – Multiple appenders using XML configuration Rating: 5 Reviewed By: Annamalai Thangaraj