📄 hierarchy.java
字号:
logger.setHierarchy(this); ht.put(key, logger); updateChildren((ProvisionNode) o, logger); updateParents(logger); return logger; } else { // It should be impossible to arrive here return null; // but let's keep the compiler happy. } } } /** Returns all the currently defined categories in this hierarchy as an {@link java.util.Enumeration Enumeration}. <p>The root logger is <em>not</em> included in the returned {@link Enumeration}. */ public Enumeration getCurrentLoggers() { // The accumlation in v is necessary because not all elements in // ht are Logger objects as there might be some ProvisionNodes // as well. Vector v = new Vector(ht.size()); Enumeration elems = ht.elements(); while(elems.hasMoreElements()) { Object o = elems.nextElement(); if(o instanceof Logger) { v.addElement(o); } } return v.elements(); } /** @deprecated Please use {@link #getCurrentLoggers} instead. */ public Enumeration getCurrentCategories() { return getCurrentLoggers(); } /** Get the renderer map for this hierarchy. */ public RendererMap getRendererMap() { return rendererMap; } /** Get the root of this hierarchy. @since 0.9.0 */ public Logger getRootLogger() { return root; } /** This method will return <code>true</code> if this repository is disabled for <code>level</code> object passed as parameter and <code>false</code> otherwise. See also the {@link #setThreshold(Level) threshold} emthod. */ public boolean isDisabled(int level) { return thresholdInt > level; } /** @deprecated Deprecated with no replacement. */ public void overrideAsNeeded(String override) { LogLog.warn("The Hiearchy.overrideAsNeeded method has been deprecated."); } /** Reset all values contained in this hierarchy instance to their default. This removes all appenders from all categories, sets the level of all non-root categories to <code>null</code>, sets their additivity flag to <code>true</code> and sets the level of the root logger to {@link Level#DEBUG DEBUG}. Moreover, message disabling is set its default "off" value. <p>Existing categories are not removed. They are just reset. <p>This method should be used sparingly and with care as it will block all logging until it is completed.</p> @since 0.8.5 */ public void resetConfiguration() { getRootLogger().setLevel((Level) Level.DEBUG); root.setResourceBundle(null); setThreshold(Level.ALL); // the synchronization is needed to prevent JDK 1.2.x hashtable // surprises synchronized(ht) { shutdown(); // nested locks are OK Enumeration cats = getCurrentLoggers(); while(cats.hasMoreElements()) { Logger c = (Logger) cats.nextElement(); c.setLevel(null); c.setAdditivity(true); c.setResourceBundle(null); } } rendererMap.clear(); } /** Does mothing. @deprecated Deprecated with no replacement. */ public void setDisableOverride(String override) { LogLog.warn("The Hiearchy.setDisableOverride method has been deprecated."); } /** Used by subclasses to add a renderer to the hierarchy passed as parameter. */ public void setRenderer(Class renderedClass, ObjectRenderer renderer) { rendererMap.put(renderedClass, renderer); } /** Shutting down a hierarchy will <em>safely</em> close and remove all appenders in all categories including the root logger. <p>Some appenders such as {@link org.apache.log4j.net.SocketAppender} and {@link AsyncAppender} need to be closed before the application exists. Otherwise, pending logging events might be lost. <p>The <code>shutdown</code> method is careful to close nested appenders before closing regular appenders. This is allows configurations where a regular appender is attached to a logger and again to a nested appender. @since 1.0 */ public void shutdown() { Logger root = getRootLogger(); // begin by closing nested appenders root.closeNestedAppenders(); synchronized(ht) { Enumeration cats = this.getCurrentLoggers(); while(cats.hasMoreElements()) { Logger c = (Logger) cats.nextElement(); c.closeNestedAppenders(); } // then, remove all appenders root.removeAllAppenders(); cats = this.getCurrentLoggers(); while(cats.hasMoreElements()) { Logger c = (Logger) cats.nextElement(); c.removeAllAppenders(); } } } /** This method loops through all the *potential* parents of 'cat'. There 3 possible cases: 1) No entry for the potential parent of 'cat' exists We create a ProvisionNode for this potential parent and insert 'cat' in that provision node. 2) There entry is of type Logger for the potential parent. The entry is 'cat's nearest existing parent. We update cat's parent field with this entry. We also break from the loop because updating our parent's parent is our parent's responsibility. 3) There entry is of type ProvisionNode for this potential parent. We add 'cat' to the list of children for this potential parent. */ final private void updateParents(Logger cat) { String name = cat.name; int length = name.length(); boolean parentFound = false; //System.out.println("UpdateParents called for " + name); // if name = "w.x.y.z", loop thourgh "w.x.y", "w.x" and "w", but not "w.x.y.z" for(int i = name.lastIndexOf('.', length-1); i >= 0; i = name.lastIndexOf('.', i-1)) { String substr = name.substring(0, i); //System.out.println("Updating parent : " + substr); CategoryKey key = new CategoryKey(substr); // simple constructor Object o = ht.get(key); // Create a provision node for a future parent. if(o == null) { //System.out.println("No parent "+substr+" found. Creating ProvisionNode."); ProvisionNode pn = new ProvisionNode(cat); ht.put(key, pn); } else if(o instanceof Category) { parentFound = true; cat.parent = (Category) o; //System.out.println("Linking " + cat.name + " -> " + ((Category) o).name); break; // no need to update the ancestors of the closest ancestor } else if(o instanceof ProvisionNode) { ((ProvisionNode) o).addElement(cat); } else { Exception e = new IllegalStateException("unexpected object type " + o.getClass() + " in ht."); e.printStackTrace(); } } // If we could not find any existing parents, then link with root. if(!parentFound) cat.parent = root; } /** We update the links for all the children that placed themselves in the provision node 'pn'. The second argument 'cat' is a reference for the newly created Logger, parent of all the children in 'pn' We loop on all the children 'c' in 'pn': If the child 'c' has been already linked to a child of 'cat' then there is no need to update 'c'. Otherwise, we set cat's parent field to c's parent and set c's parent field to cat. */ final private void updateChildren(ProvisionNode pn, Logger logger) { //System.out.println("updateChildren called for " + logger.name); final int last = pn.size(); for(int i = 0; i < last; i++) { Logger l = (Logger) pn.elementAt(i); //System.out.println("Updating child " +p.name); // Unless this child already points to a correct (lower) parent, // make cat.parent point to l.parent and l.parent to cat. if(!l.parent.name.startsWith(logger.name)) { logger.parent = l.parent; l.parent = logger; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -