1 16 17 package org.apache.log4j; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.io.OutputStream ; 22 import java.io.OutputStreamWriter ; 23 24 import org.apache.log4j.spi.ErrorHandler; 25 import org.apache.log4j.spi.LoggingEvent; 26 import org.apache.log4j.helpers.QuietWriter; 27 import org.apache.log4j.helpers.LogLog; 28 29 32 38 public class WriterAppender extends AppenderSkeleton { 39 40 41 54 protected boolean immediateFlush = true; 55 56 61 protected String encoding; 62 63 67 protected QuietWriter qw; 68 69 70 72 public 73 WriterAppender() { 74 } 75 76 80 public 81 WriterAppender(Layout layout, OutputStream os) { 82 this(layout, new OutputStreamWriter (os)); 83 } 84 85 91 public 92 WriterAppender(Layout layout, Writer writer) { 93 this.layout = layout; 94 this.setWriter(writer); 95 } 96 97 111 public 112 void setImmediateFlush(boolean value) { 113 immediateFlush = value; 114 } 115 116 119 public 120 boolean getImmediateFlush() { 121 return immediateFlush; 122 } 123 124 127 public 128 void activateOptions() { 129 } 130 131 132 144 public 145 void append(LoggingEvent event) { 146 147 156 if(!checkEntryConditions()) { 157 return; 158 } 159 subAppend(event); 160 } 161 162 168 protected 169 boolean checkEntryConditions() { 170 if(this.closed) { 171 LogLog.warn("Not allowed to write to a closed appender."); 172 return false; 173 } 174 175 if(this.qw == null) { 176 errorHandler.error("No output stream or file set for the appender named ["+ 177 name+"]."); 178 return false; 179 } 180 181 if(this.layout == null) { 182 errorHandler.error("No layout set for the appender named ["+ name+"]."); 183 return false; 184 } 185 return true; 186 } 187 188 189 197 public 198 synchronized 199 void close() { 200 if(this.closed) 201 return; 202 this.closed = true; 203 writeFooter(); 204 reset(); 205 } 206 207 210 protected void closeWriter() { 211 if(qw != null) { 212 try { 213 qw.close(); 214 } catch(IOException e) { 215 LogLog.error("Could not close " + qw, e); 218 } 219 } 220 } 221 222 228 protected 229 OutputStreamWriter createWriter(OutputStream os) { 230 OutputStreamWriter retval = null; 231 232 String enc = getEncoding(); 233 if(enc != null) { 234 try { 235 retval = new OutputStreamWriter (os, enc); 236 } catch(IOException e) { 237 LogLog.warn("Error initializing output writer."); 238 LogLog.warn("Unsupported encoding?"); 239 } 240 } 241 if(retval == null) { 242 retval = new OutputStreamWriter (os); 243 } 244 return retval; 245 } 246 247 public String getEncoding() { 248 return encoding; 249 } 250 251 public void setEncoding(String value) { 252 encoding = value; 253 } 254 255 256 257 258 261 public synchronized void setErrorHandler(ErrorHandler eh) { 262 if(eh == null) { 263 LogLog.warn("You have tried to set a null error-handler."); 264 } else { 265 this.errorHandler = eh; 266 if(this.qw != null) { 267 this.qw.setErrorHandler(eh); 268 } 269 } 270 } 271 272 284 public synchronized void setWriter(Writer writer) { 285 reset(); 286 this.qw = new QuietWriter(writer, errorHandler); 287 writeHeader(); 289 } 290 291 292 299 protected 300 void subAppend(LoggingEvent event) { 301 this.qw.write(this.layout.format(event)); 302 303 if(layout.ignoresThrowable()) { 304 String [] s = event.getThrowableStrRep(); 305 if (s != null) { 306 int len = s.length; 307 for(int i = 0; i < len; i++) { 308 this.qw.write(s[i]); 309 this.qw.write(Layout.LINE_SEP); 310 } 311 } 312 } 313 314 if(this.immediateFlush) { 315 this.qw.flush(); 316 } 317 } 318 319 320 321 325 public 326 boolean requiresLayout() { 327 return true; 328 } 329 330 335 protected 336 void reset() { 337 closeWriter(); 338 this.qw = null; 339 } 341 342 343 346 protected 347 void writeFooter() { 348 if(layout != null) { 349 String f = layout.getFooter(); 350 if(f != null && this.qw != null) { 351 this.qw.write(f); 352 this.qw.flush(); 353 } 354 } 355 } 356 357 360 protected 361 void writeHeader() { 362 if(layout != null) { 363 String h = layout.getHeader(); 364 if(h != null && this.qw != null) 365 this.qw.write(h); 366 } 367 } 368 } 369 | Popular Tags |