1 16 17 20 package org.apache.log4j; 21 22 import org.apache.log4j.spi.LoggingEvent; 23 import org.apache.log4j.helpers.BoundedFIFO; 24 import org.apache.log4j.spi.AppenderAttachable; 25 import org.apache.log4j.helpers.AppenderAttachableImpl; 26 import org.apache.log4j.helpers.LogLog; 27 import java.util.Enumeration ; 28 29 49 public class AsyncAppender extends AppenderSkeleton 50 implements AppenderAttachable { 51 52 53 public static final int DEFAULT_BUFFER_SIZE = 128; 54 55 57 BoundedFIFO bf = new BoundedFIFO(DEFAULT_BUFFER_SIZE); 58 59 AppenderAttachableImpl aai; 60 Dispatcher dispatcher; 61 boolean locationInfo = false; 62 63 boolean interruptedWarningMessage = false; 64 65 public AsyncAppender() { 66 aai = new AppenderAttachableImpl(); 69 dispatcher = new Dispatcher(bf, this); 70 dispatcher.start(); 71 } 72 73 74 public void addAppender(Appender newAppender) { 75 synchronized(aai) { 76 aai.addAppender(newAppender); 77 } 78 } 79 80 public void append(LoggingEvent event) { 81 event.getNDC(); 84 event.getThreadName(); 85 event.getMDCCopy(); 87 if(locationInfo) { 88 event.getLocationInformation(); 89 } 90 synchronized(bf) { 91 while(bf.isFull()) { 92 try { 93 bf.wait(); 95 } catch(InterruptedException e) { 96 if(!interruptedWarningMessage) { 97 interruptedWarningMessage = true; 98 LogLog.warn("AsyncAppender interrupted.", e); 99 } else { 100 LogLog.warn("AsyncAppender interrupted again."); 101 } 102 } 103 } 104 105 bf.put(event); 107 if(bf.wasEmpty()) { 108 bf.notify(); 110 } 111 } 112 } 113 114 119 public void close() { 120 synchronized(this) { 121 if(closed) { 123 return; 124 } 125 closed = true; 126 } 127 128 dispatcher.close(); 133 try { 134 dispatcher.join(); 135 } catch(InterruptedException e) { 136 LogLog.error("Got an InterruptedException while waiting for the "+ 137 "dispatcher to finish.", e); 138 } 139 dispatcher = null; 140 bf = null; 141 } 142 143 public Enumeration getAllAppenders() { 144 synchronized(aai) { 145 return aai.getAllAppenders(); 146 } 147 } 148 149 public Appender getAppender(String name) { 150 synchronized(aai) { 151 return aai.getAppender(name); 152 } 153 } 154 155 158 public boolean getLocationInfo() { 159 return locationInfo; 160 } 161 162 165 public boolean isAttached(Appender appender) { 166 return aai.isAttached(appender); 167 } 168 169 170 174 public boolean requiresLayout() { 175 return false; 176 } 177 178 public void removeAllAppenders() { 179 synchronized(aai) { 180 aai.removeAllAppenders(); 181 } 182 } 183 184 185 public void removeAppender(Appender appender) { 186 synchronized(aai) { 187 aai.removeAppender(appender); 188 } 189 } 190 191 public void removeAppender(String name) { 192 synchronized(aai) { 193 aai.removeAppender(name); 194 } 195 } 196 197 207 public void setLocationInfo(boolean flag) { 208 locationInfo = flag; 209 } 210 211 212 224 public void setBufferSize(int size) { 225 bf.resize(size); 226 } 227 228 231 public int getBufferSize() { 232 return bf.getMaxSize(); 233 } 234 235 } 236 class Dispatcher extends Thread { 240 241 BoundedFIFO bf; 242 AppenderAttachableImpl aai; 243 boolean interrupted = false; 244 AsyncAppender container; 245 246 Dispatcher(BoundedFIFO bf, AsyncAppender container) { 247 this.bf = bf; 248 this.container = container; 249 this.aai = container.aai; 250 this.setDaemon(true); 253 this.setPriority(Thread.MIN_PRIORITY); 255 this.setName("Dispatcher-"+getName()); 256 257 261 } 262 263 void close() { 264 synchronized(bf) { 265 interrupted = true; 266 if(bf.length() == 0) { 269 bf.notify(); 270 } 271 } 272 } 273 274 275 276 286 public void run() { 287 288 290 LoggingEvent event; 291 292 while(true) { 293 synchronized(bf) { 294 if(bf.length() == 0) { 295 if(interrupted) { 297 break; 299 } 300 try { 301 bf.wait(); 303 } catch(InterruptedException e) { 304 LogLog.error("The dispathcer should not be interrupted."); 305 break; 306 } 307 } 308 event = bf.get(); 309 if(bf.wasFull()) { 310 bf.notify(); 312 } 313 } 315 synchronized(container.aai) { 318 if(aai != null && event != null) { 319 aai.appendLoopOnAppenders(event); 320 } 321 } 322 } 324 aai.removeAllAppenders(); 326 } 327 } 328 | Popular Tags |