1 16 17 package org.apache.log4j; 18 19 import org.apache.log4j.Layout; 20 import org.apache.log4j.spi.Filter; 21 import org.apache.log4j.spi.ErrorHandler; 22 import org.apache.log4j.spi.OptionHandler; 23 import org.apache.log4j.spi.LoggingEvent; 24 import org.apache.log4j.helpers.OnlyOnceErrorHandler; 25 import org.apache.log4j.helpers.LogLog; 26 27 28 37 public abstract class AppenderSkeleton implements Appender, OptionHandler { 38 39 41 protected Layout layout; 42 43 44 protected String name; 45 46 48 protected Priority threshold; 49 50 53 protected ErrorHandler errorHandler = new OnlyOnceErrorHandler(); 54 55 57 protected Filter headFilter; 58 59 protected Filter tailFilter; 60 61 64 protected boolean closed = false; 65 66 67 70 public 71 void activateOptions() { 72 } 73 74 75 80 public 81 void addFilter(Filter newFilter) { 82 if(headFilter == null) { 83 headFilter = tailFilter = newFilter; 84 } else { 85 tailFilter.next = newFilter; 86 tailFilter = newFilter; 87 } 88 } 89 90 97 abstract 98 protected 99 void append(LoggingEvent event); 100 101 102 106 public 107 void clearFilters() { 108 headFilter = tailFilter = null; 109 } 110 111 116 public 117 void finalize() { 118 if(this.closed) 121 return; 122 123 LogLog.debug("Finalizing appender named ["+name+"]."); 124 close(); 125 } 126 127 128 133 public 134 ErrorHandler getErrorHandler() { 135 return this.errorHandler; 136 } 137 138 139 144 public 145 Filter getFilter() { 146 return headFilter; 147 } 148 149 155 public 156 final 157 Filter getFirstFilter() { 158 return headFilter; 159 } 160 161 164 public 165 Layout getLayout() { 166 return layout; 167 } 168 169 170 173 public 174 final 175 String getName() { 176 return this.name; 177 } 178 179 184 public 185 Priority getThreshold() { 186 return threshold; 187 } 188 189 190 196 public 197 boolean isAsSevereAsThreshold(Priority priority) { 198 return ((threshold == null) || priority.isGreaterOrEqual(threshold)); 199 } 200 201 202 207 public 208 synchronized 209 void doAppend(LoggingEvent event) { 210 if(closed) { 211 LogLog.error("Attempted to append to closed appender named ["+name+"]."); 212 return; 213 } 214 215 if(!isAsSevereAsThreshold(event.getLevel())) { 216 return; 217 } 218 219 Filter f = this.headFilter; 220 221 FILTER_LOOP: 222 while(f != null) { 223 switch(f.decide(event)) { 224 case Filter.DENY: return; 225 case Filter.ACCEPT: break FILTER_LOOP; 226 case Filter.NEUTRAL: f = f.next; 227 } 228 } 229 230 this.append(event); 231 } 232 233 237 public 238 synchronized 239 void setErrorHandler(ErrorHandler eh) { 240 if(eh == null) { 241 LogLog.warn("You have tried to set a null error-handler."); 244 } else { 245 this.errorHandler = eh; 246 } 247 } 248 249 255 public 256 void setLayout(Layout layout) { 257 this.layout = layout; 258 } 259 260 261 264 public 265 void setName(String name) { 266 this.name = name; 267 } 268 269 270 279 public 280 void setThreshold(Priority threshold) { 281 this.threshold = threshold; 282 } 283 } 284 | Popular Tags |