I have a spring batch program where I am implementing skiplistener class as below:
public class MySkipListener implements SkipListener<SomeBean, SomeBean> {
public void onSkipInProcess(final SomeBean item, final Throwable t) {
// TODO Auto-generated method stub
System.out.println("Skipped details during PROCESS is: " + t.getMessage());
LogFactory.getLog("INFO" + MySkipListener.class.getName()).info(
"Skipped details during PROCESS is: " + t.getMessage());
}
public void onSkipInRead(final Throwable t) {
System.out.println("Skipped details during READ is: " + t.getMessage());
LogFactory.getLog("INFO" + MySkipListener.class.getName()).info(
"Skipped details during READ is: " + t.getMessage());
}
public void onSkipInWrite(final SomeBean item, final Throwable t) {
// TODO Auto-generated method stub
System.out.println("Skipped details during WRITE is: " + t.getMessage());
LogFactory.getLog("INFO" + MySkipListener.class.getName()).info(
"Skipped details during WRITE is: " + t.getMessage());
}
}
The log4j config is:
log4j.rootLogger=myLOG
log4j.appender.myLOG=org.apache.log4j.RollingFileAppender
log4j.appender.myLOG.File=myLog.log
log4j.appender.myLOG.MaxFileSize=100KB
# Keep one backup file
log4j.appender.myLOG.MaxBackupIndex=1
log4j.appender.myLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.myLOG.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} [%t] [%c{1}] %-5p %x - %m%n
As clear all the logging statements goes into myLog.log including the statements from above skip listener class methods.
However, I do not want the skipped items logs going into the main log file. I want them to go to a separate file (e.g. skippedItems.log) so that I need not search for the skipped items line in the main log but get all the skipped items directly in a separate file.
What changes need I do for achieving this?
Below try is not working where myLOGTwo is a new appender in my config which is not added into rootLogger. So I want to add and remove it to rootLogger dynamically everytime I want to write through that appender.
Logger l = Logger.getRootLogger();
Appender a = l.getAppender("myLOGTwo");
l.addAppender(a);
LogFactory.getLog("INFO" + MySkipListener.class.getName()).info("Testing");
l.removeAppender(a);
Please see the comment from Michael in the question above…