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

Log4j – RollingFileAppender using XML configuration


In this tutorials we are going to see how to create simple log4j application with RollingFileAppender using XML configuration
RollingFileAppeder
RollingFileAppender is used to create log message in multiple files,RollingFileAppeder appender similar to FileAppender.RollingFileAppeder  support two additional configuration.
Property Description
maxFileSize This is used to set maximum log file size,when log file reaches the maximum size file will be rolled out.Default value is 10MB
maxBackupIndex This is used to set maximum number of backup file.when total no of file reaches maxBackupIndex first created file will be erased. Default value is 1.
Create java project called Log4jExample in your Eclipse IDE, then create package called com.javatutorialscorner.log4j.rollingfileappender 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.RollingFileAppender">
<param name="file" value="${user.home}/rollingFile.log" />
<param name="maxFileSize" value="10kb" />
<param name="maxBackupIndex" value="2" />
<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 used to configure Level and Appender, in this example I used DEBUG as Level and RollingFileAppender is used as appender(FILE is name of appender).maxFileSize is used to set maximum size of log file,when log file reaches the maximum size old log file will be rolled out to new log file will be created,old file saved as backup. maxBackupIndex is set to maximum no of back file is saved on disk,when total number  reaches this index oldest file will be erased .

PatternLayout is used to configure log4j message format.

Now create java class called RollingFileAppeder under com.javatutorialscorner.log4j.rollingfileappender package.

RollingFileAppeder .java

package com.javatutorialscorner.log4j.rollingfileappender;

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

public class RollingFileAppeder {

private static final String FILE_PATH = "log4j.xml";
private static final Logger log = Logger
.getLogger(RollingFileAppeder.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(RollingFileAppeder.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] DEBUG com.javatutorialscorner.log4j.rollingfileappender.RollingFileAppeder  - Debug message by Java Tutorials Corner

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


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


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


0 [main] FATAL com.javatutorialscorner.log4j.rollingfileappender.RollingFileAppeder  - 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 – RollingFileAppender using XML configuration Rating: 5 Reviewed By: eHowToNow