1 16 17 package org.apache.log4j; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.io.FileOutputStream ; 22 import java.io.BufferedWriter ; 23 24 import org.apache.log4j.spi.ErrorCode; 25 import org.apache.log4j.helpers.QuietWriter; 26 import org.apache.log4j.helpers.LogLog; 27 28 31 40 public class FileAppender extends WriterAppender { 41 42 50 protected boolean fileAppend = true; 51 52 54 protected String fileName = null; 55 56 58 protected boolean bufferedIO = false; 59 60 63 protected int bufferSize = 8*1024; 64 65 66 69 public 70 FileAppender() { 71 } 72 73 86 public 87 FileAppender(Layout layout, String filename, boolean append, boolean bufferedIO, 88 int bufferSize) throws IOException { 89 this.layout = layout; 90 this.setFile(filename, append, bufferedIO, bufferSize); 91 } 92 93 102 public 103 FileAppender(Layout layout, String filename, boolean append) 104 throws IOException { 105 this.layout = layout; 106 this.setFile(filename, append, false, bufferSize); 107 } 108 109 115 public 116 FileAppender(Layout layout, String filename) throws IOException { 117 this(layout, filename, true); 118 } 119 120 129 public void setFile(String file) { 130 String val = file.trim(); 133 fileName = val; 134 } 135 136 139 public 140 boolean getAppend() { 141 return fileAppend; 142 } 143 144 145 146 public 147 String getFile() { 148 return fileName; 149 } 150 151 157 public 158 void activateOptions() { 159 if(fileName != null) { 160 try { 161 setFile(fileName, fileAppend, bufferedIO, bufferSize); 162 } 163 catch(java.io.IOException e) { 164 errorHandler.error("setFile("+fileName+","+fileAppend+") call failed.", 165 e, ErrorCode.FILE_OPEN_FAILURE); 166 } 167 } else { 168 LogLog.warn("File option not set for appender ["+name+"]."); 170 LogLog.warn("Are you using FileAppender instead of ConsoleAppender?"); 171 } 172 } 173 174 177 protected 178 void closeFile() { 179 if(this.qw != null) { 180 try { 181 this.qw.close(); 182 } 183 catch(java.io.IOException e) { 184 LogLog.error("Could not close " + qw, e); 187 } 188 } 189 } 190 191 198 public 199 boolean getBufferedIO() { 200 return this.bufferedIO; 201 } 202 203 204 207 public 208 int getBufferSize() { 209 return this.bufferSize; 210 } 211 212 213 214 224 public 225 void setAppend(boolean flag) { 226 fileAppend = flag; 227 } 228 229 239 public 240 void setBufferedIO(boolean bufferedIO) { 241 this.bufferedIO = bufferedIO; 242 if(bufferedIO) { 243 immediateFlush = false; 244 } 245 } 246 247 248 251 public 252 void setBufferSize(int bufferSize) { 253 this.bufferSize = bufferSize; 254 } 255 256 270 public 271 synchronized 272 void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) 273 throws IOException { 274 LogLog.debug("setFile called: "+fileName+", "+append); 275 276 if(bufferedIO) { 278 setImmediateFlush(false); 279 } 280 281 reset(); 282 Writer fw = createWriter(new FileOutputStream (fileName, append)); 283 if(bufferedIO) { 284 fw = new BufferedWriter (fw, bufferSize); 285 } 286 this.setQWForFiles(fw); 287 this.fileName = fileName; 288 this.fileAppend = append; 289 this.bufferedIO = bufferedIO; 290 this.bufferSize = bufferSize; 291 writeHeader(); 292 LogLog.debug("setFile ended"); 293 } 294 295 296 301 protected 302 void setQWForFiles(Writer writer) { 303 this.qw = new QuietWriter(writer, errorHandler); 304 } 305 306 307 310 protected 311 void reset() { 312 closeFile(); 313 this.fileName = null; 314 super.reset(); 315 } 316 } 317 318 | Popular Tags |