In this tutorials we are going to see how to create simple log4j application with RollingFileAppender using property 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. |
log4j.properties
#The root logger with appender file
logFile = C:/log
log4j.rootLogger = DEBUG, FILE
#The file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${logFile}/rollingFile.log
# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=10KB
# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=5
#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 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.PropertyConfigurator;
public class RollingFileAppeder {
private static final String FILE_PATH = "log4j.properties";
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 {
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(RollingFileAppeder.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 file at specified location.
0 [main] INFO com.javatutorialscorner.log4j.rollingfileappender.RollingFileAppeder - Log4j Configured using Property Configurator
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
0 [main] INFO com.javatutorialscorner.log4j.rollingfileappender.RollingFileAppeder - Log4j Configured using Property Configurator
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
0 [main] INFO com.javatutorialscorner.log4j.rollingfileappender.RollingFileAppeder - Log4j Configured using Property Configurator
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
0 comments:
Post a Comment