1 16 17 21 27 package org.apache.log4j; 28 29 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 import java.util.Vector ; 33 34 import org.apache.log4j.spi.LoggerFactory; 35 import org.apache.log4j.spi.HierarchyEventListener; 36 import org.apache.log4j.spi.LoggerRepository; 37 import org.apache.log4j.spi.RendererSupport; 38 import org.apache.log4j.Appender; 39 import org.apache.log4j.or.RendererMap; 40 import org.apache.log4j.or.ObjectRenderer; 41 import org.apache.log4j.helpers.LogLog; 42 43 64 public class Hierarchy implements LoggerRepository, RendererSupport { 65 66 private LoggerFactory defaultFactory; 67 private Vector listeners; 68 69 Hashtable ht; 70 Logger root; 71 RendererMap rendererMap; 72 73 int thresholdInt; 74 Level threshold; 75 76 boolean emittedNoAppenderWarning = false; 77 boolean emittedNoResourceBundleWarning = false; 78 79 85 public 86 Hierarchy(Logger root) { 87 ht = new Hashtable (); 88 listeners = new Vector (1); 89 this.root = root; 90 setThreshold(Level.ALL); 92 this.root.setHierarchy(this); 93 rendererMap = new RendererMap(); 94 defaultFactory = new DefaultCategoryFactory(); 95 } 96 97 100 public 101 void addRenderer(Class classToRender, ObjectRenderer or) { 102 rendererMap.put(classToRender, or); 103 } 104 105 public 106 void addHierarchyEventListener(HierarchyEventListener listener) { 107 if(listeners.contains(listener)) { 108 LogLog.warn("Ignoring attempt to add an existent listener."); 109 } else { 110 listeners.addElement(listener); 111 } 112 } 113 114 123 public 124 void clear() { 125 ht.clear(); 127 } 128 129 public 130 void emitNoAppenderWarning(Category cat) { 131 if(!this.emittedNoAppenderWarning) { 133 LogLog.warn("No appenders could be found for logger (" + 134 cat.getName() + ")."); 135 LogLog.warn("Please initialize the log4j system properly."); 136 this.emittedNoAppenderWarning = true; 137 } 138 } 139 140 147 public 148 Logger exists(String name) { 149 Object o = ht.get(new CategoryKey(name)); 150 if(o instanceof Logger) { 151 return (Logger) o; 152 } else { 153 return null; 154 } 155 } 156 157 160 public 161 void setThreshold(String levelStr) { 162 Level l = (Level) Level.toLevel(levelStr, null); 163 if(l != null) { 164 setThreshold(l); 165 } else { 166 LogLog.warn("Could not convert ["+levelStr+"] to Level."); 167 } 168 } 169 170 171 177 public 178 void setThreshold(Level l) { 179 if(l != null) { 180 thresholdInt = l.level; 181 threshold = l; 182 } 183 } 184 185 public 186 void fireAddAppenderEvent(Category logger, Appender appender) { 187 if(listeners != null) { 188 int size = listeners.size(); 189 HierarchyEventListener listener; 190 for(int i = 0; i < size; i++) { 191 listener = (HierarchyEventListener) listeners.elementAt(i); 192 listener.addAppenderEvent(logger, appender); 193 } 194 } 195 } 196 197 void fireRemoveAppenderEvent(Category logger, Appender appender) { 198 if(listeners != null) { 199 int size = listeners.size(); 200 HierarchyEventListener listener; 201 for(int i = 0; i < size; i++) { 202 listener = (HierarchyEventListener) listeners.elementAt(i); 203 listener.removeAppenderEvent(logger, appender); 204 } 205 } 206 } 207 208 213 public 214 Level getThreshold() { 215 return threshold; 216 } 217 218 223 228 229 240 public 241 Logger getLogger(String name) { 242 return getLogger(name, defaultFactory); 243 } 244 245 258 public 259 Logger getLogger(String name, LoggerFactory factory) { 260 CategoryKey key = new CategoryKey(name); 262 Logger logger; 266 267 synchronized(ht) { 268 Object o = ht.get(key); 269 if(o == null) { 270 logger = factory.makeNewLoggerInstance(name); 271 logger.setHierarchy(this); 272 ht.put(key, logger); 273 updateParents(logger); 274 return logger; 275 } else if(o instanceof Logger) { 276 return (Logger) o; 277 } else if (o instanceof ProvisionNode) { 278 logger = factory.makeNewLoggerInstance(name); 280 logger.setHierarchy(this); 281 ht.put(key, logger); 282 updateChildren((ProvisionNode) o, logger); 283 updateParents(logger); 284 return logger; 285 } 286 else { 287 return null; } 290 } 291 } 292 293 299 public 300 Enumeration getCurrentLoggers() { 301 Vector v = new Vector (ht.size()); 305 306 Enumeration elems = ht.elements(); 307 while(elems.hasMoreElements()) { 308 Object o = elems.nextElement(); 309 if(o instanceof Logger) { 310 v.addElement(o); 311 } 312 } 313 return v.elements(); 314 } 315 316 319 public 320 Enumeration getCurrentCategories() { 321 return getCurrentLoggers(); 322 } 323 324 325 328 public 329 RendererMap getRendererMap() { 330 return rendererMap; 331 } 332 333 334 339 public 340 Logger getRootLogger() { 341 return root; 342 } 343 344 349 public 350 boolean isDisabled(int level) { 351 return thresholdInt > level; 352 } 353 354 357 public 358 void overrideAsNeeded(String override) { 359 LogLog.warn("The Hiearchy.overrideAsNeeded method has been deprecated."); 360 } 361 362 376 public 377 void resetConfiguration() { 378 379 getRootLogger().setLevel((Level) Level.DEBUG); 380 root.setResourceBundle(null); 381 setThreshold(Level.ALL); 382 383 synchronized(ht) { 386 shutdown(); 388 Enumeration cats = getCurrentLoggers(); 389 while(cats.hasMoreElements()) { 390 Logger c = (Logger) cats.nextElement(); 391 c.setLevel(null); 392 c.setAdditivity(true); 393 c.setResourceBundle(null); 394 } 395 } 396 rendererMap.clear(); 397 } 398 399 404 public 405 void setDisableOverride(String override) { 406 LogLog.warn("The Hiearchy.setDisableOverride method has been deprecated."); 407 } 408 409 410 411 414 public 415 void setRenderer(Class renderedClass, ObjectRenderer renderer) { 416 rendererMap.put(renderedClass, renderer); 417 } 418 419 420 436 public 437 void shutdown() { 438 Logger root = getRootLogger(); 439 440 root.closeNestedAppenders(); 442 443 synchronized(ht) { 444 Enumeration cats = this.getCurrentLoggers(); 445 while(cats.hasMoreElements()) { 446 Logger c = (Logger) cats.nextElement(); 447 c.closeNestedAppenders(); 448 } 449 450 root.removeAllAppenders(); 452 cats = this.getCurrentLoggers(); 453 while(cats.hasMoreElements()) { 454 Logger c = (Logger) cats.nextElement(); 455 c.removeAllAppenders(); 456 } 457 } 458 } 459 460 461 481 final 482 private 483 void updateParents(Logger cat) { 484 String name = cat.name; 485 int length = name.length(); 486 boolean parentFound = false; 487 488 490 for(int i = name.lastIndexOf('.', length-1); i >= 0; 492 i = name.lastIndexOf('.', i-1)) { 493 String substr = name.substring(0, i); 494 495 CategoryKey key = new CategoryKey(substr); Object o = ht.get(key); 498 if(o == null) { 500 ProvisionNode pn = new ProvisionNode(cat); 502 ht.put(key, pn); 503 } else if(o instanceof Category) { 504 parentFound = true; 505 cat.parent = (Category) o; 506 break; } else if(o instanceof ProvisionNode) { 509 ((ProvisionNode) o).addElement(cat); 510 } else { 511 Exception e = new IllegalStateException ("unexpected object type " + 512 o.getClass() + " in ht."); 513 e.printStackTrace(); 514 } 515 } 516 if(!parentFound) 518 cat.parent = root; 519 } 520 521 536 final 537 private 538 void updateChildren(ProvisionNode pn, Logger logger) { 539 final int last = pn.size(); 541 542 for(int i = 0; i < last; i++) { 543 Logger l = (Logger) pn.elementAt(i); 544 546 if(!l.parent.name.startsWith(logger.name)) { 549 logger.parent = l.parent; 550 l.parent = logger; 551 } 552 } 553 } 554 555 } 556 557 558 | Popular Tags |