Friday, July 1, 2016

Creating a log for each run/Custom file appender and log different outputs details to different files

Here i explain how to create a log for each run. Basically by adding a custom appender.
next i need to log different details to different files. In simple log categories of details to different log files.

In my previous blog entry i have explained adding custom appender.
http://cgenit.blogspot.com/2016/07/writing-custom-file-appender-for-log4j_1.html

anyway I will explain again with steps

Step 1 - create java class for custom appender
Step 2 - add log4j entry for custom appender
Step 3 - add log4j entry for create another log for selected category
Step 4 -  used log4j category and write details to another log file


Step 1

======================================================
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);
}
}

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


Step 2 and Step3  - 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\\FullLog-%timestamp.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%m%n
######accountResult##############
log4j.category.accountResultLogger=DEBUG, accountResultLog
log4j.additivity.accountResultLogger=false
##
log4j.appender.accountResultLog=com.research.log.CustomFileAppender
log4j.appender.accountResultLog.File=D:\\Log\\accountResult-%timestamp.csv
log4j.appender.accountResultLog.layout=org.apache.log4j.PatternLayout
log4j.appender.accountResultLog.layout.ConversionPattern=%m%n
#####studentResultLog####
log4j.category.studentResultLogger=DEBUG, studentResultLog
log4j.additivity.studentResultLogger=false
##
log4j.appender.studentResultLog=com.research.log.CustomFileAppender
log4j.appender.studentResultLog.File=D:\\Log\\studentResult-%timestamp.csv
log4j.appender.studentResultLog.layout=org.apache.log4j.PatternLayout
log4j.appender.studentResultLog.layout.ConversionPattern=%m%n

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

Step 4

public class Logging {


final static Logger Log = Logger.getLogger(Logging.class);
final static Logger Log_accountResult = Logger.getLogger("accountResultLogger");
final static Logger Log_studentResult = Logger
.getLogger("studentResultLogger");

Log_accountResult.info("accountResult");
Log_studentResult.info("studentResult");

}

This will logging as per category in different files



No comments:

Post a Comment