日期:2014-05-18 浏览次数:21079 次
  public // synchronization not necessary since doAppend is alreasy synched
  void rollOver() {
    File target;
    File file;
    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);
    }
  }
------解决方案--------------------
有难度啊,我觉得你可以用Timer来动态生成按时间日期的文件夹,但问题怎么将log4j.appender.A2.File=C:/test/log4j/info.log改成动态的,像这样C:/{filepath}/info.log,看接下来有朋友想出好的办法吗?(其实可以使用Maven2 和 Ant来copy,但是多了步骤,麻烦);至于你那个: 2.动态日志文件编号问题 ,没必要吧,把log4j.appender.A2.MaxFileSize=20KB , MaxFileSize设大点,没什么事吧。
PS.继续关注....