将跳过的项目记录到同一弹簧批处理作业中的其他文件

时间:2012-02-03 07:45:44

标签: spring log4j logging spring-batch

我有一个Spring批处理程序,我正在实现skiplistener类,如下所示:

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

}

log4j配置为:

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

很明显,所有日志记录都会进入myLog.log,包括上面跳过侦听器类方法的语句。

但是,我不希望跳过的项目日志进入主日志文件。我希望他们转到一个单独的文件(例如skippedItems.log),这样我就不需要在主日志中搜索跳过的项目行,而是直接在一个单独的文件中获取所有跳过的项目。

我需要做些什么改变呢?

下面的try不起作用myLOGTwo是我的配置中的新appender,它没有添加到rootLogger中。因此,每当我想通过该appender编写时,我想动态添加并删除它到rootLogger。

Logger l = Logger.getRootLogger();
Appender a = l.getAppender("myLOGTwo");

l.addAppender(a);

LogFactory.getLog("INFO" + MySkipListener.class.getName()).info("Testing");

l.removeAppender(a);

1 个答案:

答案 0 :(得分:0)

请参阅上述问题中迈克尔的评论......