728x90 AdSpace

Saturday, 31 August 2013

Log4j - DailyRollingFileAppender using XML Configurator


In this tutorials we are going to see how to create simple log4j application with DailyRollingFileAppender using XML configuration
DailyRollingFileAppender
DailyRollingFileAppender is used to create log file on daily basis.DailyRollingFileAppender  extends FileAppender, inherits all property of FileAppender.DailyRollingFileAppender has one important property other than FileAppender given below.
Property Description
DatePattern This is used to set when to roll over file and naming conversion of file.By default file roll over midnight of every day.

DailyRollingFileAppender  supported DatePattern given below which is used to schedule roll over.
DatePattern Description
'.' yyyy-MM Roll over at end of each month
'.' yyyy-MM-dd Roll over at end of every mid night
'.' yyyy-MM-dd-a Roll over at end of mid day and mid night
'.' yyyy-MM-dd-HH Roll over at end of each hour
'.' yyyy-MM-dd-HH-mm Roll over at end of each minute
'.' yyyy-ww Roll over first day of every week

Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.dailyrollingfileappender 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
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${user.home}/dailyRollingFile.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%r [%t] %p %c %x - %m%n" />
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>

root tag used to configure Level and Appender, in this example I used DEBUG as Level and DailyRollingFileAppender is used as appender(FILE is name of appender).org.apache.log4j.DailyRollingFileAppender class is used to create daily rolling file,DatePattern is used to set when to roll over the file, '.' yyyy-MM-dd  this configuration will roll out end of each day, old file will be saved as backup with date at end of file name

PatternLayout is used to configure log4j message format.

Now create java class called DailyRollingFileAppender under com.javatutorialscorner.log4j.dailyrollingfileappender package.

DailyRollingFileAppender.java

package com.javatutorialscorner.log4j.dailyrollingfileappender;

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

public class DailyRollingFileAppender {

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

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (FILE_PATH == null) {
BasicConfigurator.configure();
log.info("Log4j Configured using Basic Configurator");
} else {
DOMConfigurator.configure(FILE_PATH);
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(DailyRollingFileAppender.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 file at specified location. 

0 [main] INFO com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender  - Log4j Configured using XML configurator

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


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


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


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


16 [main] FATAL com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender  - 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

0 comments:

Post a Comment

Item Reviewed: Log4j - DailyRollingFileAppender using XML Configurator Rating: 5 Reviewed By: Annamalai Thangaraj