2012-02-03 108 views
0

我有我在哪裏如下實施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是我的配置中沒有添加到rootLogger中的新appender。所以我想在每次我想通過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); 
+0

爲什麼要使用添加/ removeAppender?見例如http://www.jguru.com/faq/view.jsp?EID=1311014它適用於getLogger(「類別」) – 2012-02-11 22:11:43

回答

0

請看到邁克爾在上面的問題的評論...