1 16 17 18 19 package org.apache.log4j; 20 21 import java.io.IOException ; 22 import java.io.Writer ; 23 import java.io.File ; 24 import org.apache.log4j.helpers.OptionConverter; 25 import org.apache.log4j.helpers.LogLog; 26 import org.apache.log4j.helpers.CountingQuietWriter; 27 import org.apache.log4j.spi.LoggingEvent; 28 29 37 public class RollingFileAppender extends FileAppender { 38 39 42 protected long maxFileSize = 10*1024*1024; 43 44 47 protected int maxBackupIndex = 1; 48 49 52 public 53 RollingFileAppender() { 54 super(); 55 } 56 57 66 public 67 RollingFileAppender(Layout layout, String filename, boolean append) 68 throws IOException { 69 super(layout, filename, append); 70 } 71 72 78 public 79 RollingFileAppender(Layout layout, String filename) throws IOException { 80 super(layout, filename); 81 } 82 83 86 public 87 int getMaxBackupIndex() { 88 return maxBackupIndex; 89 } 90 91 97 public 98 long getMaximumFileSize() { 99 return maxFileSize; 100 } 101 102 116 public void rollOver() { 118 File target; 119 File file; 120 121 LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount()); 122 LogLog.debug("maxBackupIndex="+maxBackupIndex); 123 124 if(maxBackupIndex > 0) { 126 file = new File (fileName + '.' + maxBackupIndex); 128 if (file.exists()) 129 file.delete(); 130 131 for (int i = maxBackupIndex - 1; i >= 1; i--) { 133 file = new File (fileName + "." + i); 134 if (file.exists()) { 135 target = new File (fileName + '.' + (i + 1)); 136 LogLog.debug("Renaming file " + file + " to " + target); 137 file.renameTo(target); 138 } 139 } 140 141 target = new File (fileName + "." + 1); 143 144 this.closeFile(); 146 file = new File (fileName); 147 LogLog.debug("Renaming file " + file + " to " + target); 148 file.renameTo(target); 149 } 150 151 try { 152 this.setFile(fileName, false, bufferedIO, bufferSize); 155 } 156 catch(IOException e) { 157 LogLog.error("setFile("+fileName+", false) call failed.", e); 158 } 159 } 160 161 public 162 synchronized 163 void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) 164 throws IOException { 165 super.setFile(fileName, append, this.bufferedIO, this.bufferSize); 166 if(append) { 167 File f = new File (fileName); 168 ((CountingQuietWriter) qw).setCount(f.length()); 169 } 170 } 171 172 173 182 public 183 void setMaxBackupIndex(int maxBackups) { 184 this.maxBackupIndex = maxBackups; 185 } 186 187 199 public 200 void setMaximumFileSize(long maxFileSize) { 201 this.maxFileSize = maxFileSize; 202 } 203 204 205 216 public 217 void setMaxFileSize(String value) { 218 maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1); 219 } 220 221 protected 222 void setQWForFiles(Writer writer) { 223 this.qw = new CountingQuietWriter(writer, errorHandler); 224 } 225 226 232 protected 233 void subAppend(LoggingEvent event) { 234 super.subAppend(event); 235 if((fileName != null) && 236 ((CountingQuietWriter) qw).getCount() >= maxFileSize) 237 this.rollOver(); 238 } 239 } 240 | Popular Tags |