Friday, July 1, 2016

Writing a custom File Appender for log4j

log4j is a powerful and very versatile tool. here is a code snippet to write a custom file Appender.
This way you can let the log4j to create a log file for each run.
This is the biggest advantage over custom appenders in log4j

Step 1

The way i desire, First we need to write a java class for making a custom appender.Let's call it "CustomFileAppender.java"

code will be as follows

================================================================
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.FileAppender;

public class CustomFileAppender extends FileAppender {

@Override
public void setFile(String fileName) {
if (fileName.indexOf("%timestamp") >= 0) {
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSS");
fileName = fileName.replaceAll("%timestamp", format.format(d));
}
super.setFile(fileName);
}
}

=====================================================================

her we add the date and time in format "yyyyMMddHHmmssSS" after the end of file name. In otherwords as a suffix.

eg: then your log file name would be like  "log-2016063014401283"

I believe that everybody understand the numbers

for ease of everybody, i will explain that part also

the format we use "yyyyMMddHHmmssSS" and  example is "2016063014401283".
let's take part by part from left to right.


Date formatter
Description
example
yyyy
year
2016
MM
MM
06
dd
date
30
HH
hours
14
mm
minutes
40
Ss (simple)
Seconds in minute
12
SS  (capital)
Milliseconds
83

for more information  refer


let's come back to logging.

Step 2

adding appender to log4j property file  "log4j.properties"

=============================================================
# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n
####FullLog################
log4j.appender.file=com.research.log.CustomFileAppender
log4j.appender.file.File=D:\\Log\\mergeOperation-%timestamp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n

==========================================================


explanation

you can see that "log4j.rootLogger"  has "file" in line number 1

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

next line describe, how we handle the standard output when running program. eg: when you run the program in console. you can print the output as needed modifying this.This is not what we are looking for.
Creating appender for the log file, what we log actually.That is explained in the below set.
# Redirect log messages to console

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

here is how we add the custom appender.

log4j.appender.file=com.research.log.CustomFileAppender
log4j.appender.file.File=D:\\Log\\newLog-%timestamp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n

we have created file option using "CustomFileAppender.java " class. Give the name with the package except ".java"

log4j.appender.file=com.research.log.CustomFileAppender

Then we create the name of the log file in the specific destination we need.We are creating log file inside folder name "Log" in drive "D". name of the log file is "newLog.log". But as we need to ad an appender , the time as a suffix to the log name with hyphen, we give new name as  "newLog-%timestamp.log"
It will be like this

log4j.appender.file.File=D:\\Log\\newLog-%timestamp.log

next two lines describe the pattern. first you give the patter class (log4j.appender.file.layou) for the patter and after that you can decide the pattern of the log with "log4j.appender.file.layout.ConversionPattern" property.

log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n







No comments:

Post a Comment