In this tutorials we are going to see how to create simple log4j application with DailyRollingFileAppender using property 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. |
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 |
log4j.properties
#The root logger with appender file
logFile = C:/log
log4j.rootLogger = DEBUG, FILE
#The file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${logFile}/dailyRollingFile.log
# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd
#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 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.PropertyConfigurator;
public class DailyRollingFileAppender {
private static final String FILE_PATH = "log4j.properties";
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 {
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(DailyRollingFileAppender.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.dailyrollingfileappender.DailyRollingFileAppender - Log4j Configured using Property Configurator
0 [main] DEBUG com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender - Debug message by Java Tutorials Corner
0 [main] INFO com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender - Info message by Java Tutorials Corner
0 [main] WARN com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender - Warn message by Java Tutorials Corner
0 [main] ERROR com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender - Error message by Java Tutorials Corner
0 [main] FATAL com.javatutorialscorner.log4j.dailyrollingfileappender.DailyRollingFileAppender - Fatal message by Java Tutorials Corner
0 comments:
Post a Comment