FileAppender创建文件

时间:2014-02-18 09:36:42

标签: java log4j

我已经完成了FileRollerAppender的课程FileAppender。在我的新课程中,我需要重命名当前文件并从开始启动新文件。重命名文件的过程。

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

现在,当logger尝试登录“hello”时,我有异常:

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)

如何告诉记录器重新创建文件?

1 个答案:

答案 0 :(得分:0)

有理由自己创建吗? Log4j已经RollingFileAppender有几个滚动策略 - 达到最大大小,数据/小时更改和启动时。查看更多here

如果你想实现自己的,你可以看看log4j的代码,看看它是如何实现的