RollingFileAppender.java 文件源码

java
阅读 30 收藏 0 点赞 0 评论 0

项目:nabs 作者:
/**
    Implements the usual roll over behaviour.

    <p>If <code>MaxBackupIndex</code> is positive, then files
    {<code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code>}
    are renamed to {<code>File.2</code>, ...,
    <code>File.MaxBackupIndex</code>}. Moreover, <code>File</code> is
    renamed <code>File.1</code> and closed. A new <code>File</code> is
    created to receive further log output.

    <p>If <code>MaxBackupIndex</code> is equal to zero, then the
    <code>File</code> is truncated with no backup files created.

  */
 public // synchronization not necessary since doAppend is alreasy synched
 void rollOver() {
   File target;
   File file;

   if (qw != null) {
    LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount());
   }
   LogLog.debug("maxBackupIndex="+maxBackupIndex);

   // If maxBackups <= 0, then there is no file renaming to be done.
   if(maxBackupIndex > 0) {
     // Delete the oldest file, to keep Windows happy.
     file = new File(fileName + '.' + maxBackupIndex);
     if (file.exists())
      file.delete();

     // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
     for (int i = maxBackupIndex - 1; i >= 1; i--) {
file = new File(fileName + "." + i);
if (file.exists()) {
  target = new File(fileName + '.' + (i + 1));
  LogLog.debug("Renaming file " + file + " to " + target);
  file.renameTo(target);
}
     }

     // Rename fileName to fileName.1
     target = new File(fileName + "." + 1);

     this.closeFile(); // keep windows happy.

     file = new File(fileName);
     LogLog.debug("Renaming file " + file + " to " + target);
     file.renameTo(target);
   }

   try {
     // This will also close the file. This is OK since multiple
     // close operations are safe.
     this.setFile(fileName, false, bufferedIO, bufferSize);
   }
   catch(IOException e) {
     LogLog.error("setFile("+fileName+", false) call failed.", e);
   }
 }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号