2014-02-18 96 views
0

我已完成類FileRollerAppender,它延伸FileAppender。在我的新課程中,我需要重命名當前文件並從開始開始新文件。重命名文件的過程。FileAppender重新創建文件

public void rollOver() 
     { 
...   
     closeFile(); 
... 
     File file = new File(fileName); 
     file.renameTo(target); 
    }  

現在,當記錄tryes登錄 「你好」 我有例外:

log4j:ERROR Failed to write [2014-02-18 09:21:42,941 26 [main] DEBUG sys.SuperBase - hello 
]. 
java.io.IOException: Stream closed 
    at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:38) 
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:129) 
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:146) 
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:204) 
    at java.io.Writer.write(Writer.java:126) 
    at org.apache.log4j.helpers.QuietWriter.write(QuietWriter.java:39) 
    at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292) 
    at org.apache.log4j.WriterAppender.append(WriterAppender.java:150) 
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221) 
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57) 
    at org.apache.log4j.Category.callAppenders(Category.java:187) 
    at org.apache.log4j.Category.forcedLog(Category.java:372) 
    at org.apache.log4j.Category.debug(Category.java:241) 
    at sys.SuperBase.debug(SuperBase.java:93) 
    at log.FileRollerAppender.main(FileRollerAppender.java:157) 

如何分辨記錄器重新創建文件?

回答

0

是否有理由自己創建它? Log4j已經有RollingFileAppender與幾個滾動策略 - 達到最大規模,數據/小時變化和啓動。查看更多here

如果你想實現自己的,你可以看看Log4j的代碼,看看它是如何實現

+0

了super.setFile(文件名,真實,this.bufferedIO,this.bufferSize) ;來自RollingFileAppender。有用 – vico