FileDocCategorySizeDatePackage
Hierarchy.javaAPI DocApache log4j 1.2.1515968Sat Aug 25 00:09:42 BST 2007org.apache.log4j

Hierarchy

public class Hierarchy extends Object implements LoggerRepository, RendererSupport
This class is specialized in retrieving loggers by name and also maintaining the logger hierarchy.

The casual user does not have to deal with this class directly.

The structure of the logger hierarchy is maintained by the {@link #getLogger} method. The hierarchy is such that children link to their parent but parents do not have any pointers to their children. Moreover, loggers can be instantiated in any order, in particular descendant before ancestor.

In case a descendant is created before a particular ancestor, then it creates a provision node for the ancestor and adds itself to the provision node. Other descendants of the same ancestor add themselves to the previously created provision node.

author
Ceki Gülcü

Fields Summary
private LoggerFactory
defaultFactory
private Vector
listeners
Hashtable
ht
Logger
root
RendererMap
rendererMap
int
thresholdInt
Level
threshold
boolean
emittedNoAppenderWarning
boolean
emittedNoResourceBundleWarning
Constructors Summary
public Hierarchy(Logger root)
Create a new logger hierarchy.

param
root The root of the new hierarchy.


                          
  
    
    ht = new Hashtable();
    listeners = new Vector(1);
    this.root = root;
    // Enable all level levels by default.
    setThreshold(Level.ALL);
    this.root.setHierarchy(this);
    rendererMap = new RendererMap();
    defaultFactory = new DefaultCategoryFactory();
  
Methods Summary
public voidaddHierarchyEventListener(org.apache.log4j.spi.HierarchyEventListener listener)

    if(listeners.contains(listener)) {
      LogLog.warn("Ignoring attempt to add an existent listener.");
    } else {
      listeners.addElement(listener);
    }
  
public voidaddRenderer(java.lang.Class classToRender, org.apache.log4j.or.ObjectRenderer or)
Add an object renderer for a specific class.

    rendererMap.put(classToRender, or);
  
public voidclear()
This call will clear all logger definitions from the internal hashtable. Invoking this method will irrevocably mess up the logger hierarchy.

You should really know what you are doing before invoking this method.

since
0.9.0

    //System.out.println("\n\nAbout to clear internal hash table.");
    ht.clear();
  
public voidemitNoAppenderWarning(org.apache.log4j.Category cat)

    // No appenders in hierarchy, warn user only once.
    if(!this.emittedNoAppenderWarning) {
      LogLog.warn("No appenders could be found for logger (" +
		   cat.getName() + ").");
      LogLog.warn("Please initialize the log4j system properly.");
      this.emittedNoAppenderWarning = true;
    }
  
public org.apache.log4j.Loggerexists(java.lang.String name)
Check if the named logger exists in the hierarchy. If so return its reference, otherwise returns null.

param
name The name of the logger to search for.

    Object o = ht.get(new CategoryKey(name));
    if(o instanceof Logger) {
      return (Logger) o;
    } else {
      return null;
    }
  
public voidfireAddAppenderEvent(org.apache.log4j.Category logger, org.apache.log4j.Appender appender)

    if(listeners != null) {
      int size = listeners.size();
      HierarchyEventListener listener;
      for(int i = 0; i < size; i++) {
	listener = (HierarchyEventListener) listeners.elementAt(i);
	listener.addAppenderEvent(logger, appender);
      }
    }
  
voidfireRemoveAppenderEvent(org.apache.log4j.Category logger, org.apache.log4j.Appender appender)

    if(listeners != null) {
      int size = listeners.size();
      HierarchyEventListener listener;
      for(int i = 0; i < size; i++) {
	listener = (HierarchyEventListener) listeners.elementAt(i);
	listener.removeAppenderEvent(logger, appender);
      }
    }
  
public java.util.EnumerationgetCurrentCategories()

deprecated
Please use {@link #getCurrentLoggers} instead.

    return getCurrentLoggers();
  
public java.util.EnumerationgetCurrentLoggers()
Returns all the currently defined categories in this hierarchy as an {@link java.util.Enumeration Enumeration}.

The root logger is not included in the returned {@link Enumeration}.

    // 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();
  
public org.apache.log4j.LoggergetLogger(java.lang.String name)
Return a new logger instance named as the first parameter using the default factory.

If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated and then linked with its existing ancestors as well as children.

param
name The name of the logger to retrieve.

    return getLogger(name, defaultFactory);
  
public org.apache.log4j.LoggergetLogger(java.lang.String name, org.apache.log4j.spi.LoggerFactory factory)
Return a new logger instance named as the first parameter using factory.

If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated by the factory parameter and linked with its existing ancestors as well as children.

param
name The name of the logger to retrieve.
param
factory The factory that will make the new logger instance.

    //System.out.println("getInstance("+name+") called.");
    CategoryKey key = new CategoryKey(name);
    // Synchronize to prevent write conflicts. Read conflicts (in
    // getChainedLevel method) are possible only if variable
    // assignments are non-atomic.
    Logger logger;

    synchronized(ht) {
      Object o = ht.get(key);
      if(o == null) {
	logger = factory.makeNewLoggerInstance(name);
	logger.setHierarchy(this);
	ht.put(key, logger);
	updateParents(logger);
	return logger;
      } else if(o instanceof Logger) {
	return (Logger) o;
      } else if (o instanceof ProvisionNode) {
	//System.out.println("("+name+") ht.get(this) returned ProvisionNode");
	logger = factory.makeNewLoggerInstance(name);
	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.
      }
    }
  
public org.apache.log4j.or.RendererMapgetRendererMap()
Get the renderer map for this hierarchy.

    return rendererMap;
  
public org.apache.log4j.LoggergetRootLogger()
Get the root of this hierarchy.

since
0.9.0

    return root;
  
public org.apache.log4j.LevelgetThreshold()
Returns a {@link Level} representation of the enable state.

since
1.2

    return threshold;
  
public booleanisDisabled(int level)
This method will return true if this repository is disabled for level object passed as parameter and false otherwise. See also the {@link #setThreshold(Level) threshold} emthod.

    return thresholdInt > level;
  
public voidoverrideAsNeeded(java.lang.String override)

deprecated
Deprecated with no replacement.

    LogLog.warn("The Hiearchy.overrideAsNeeded method has been deprecated.");
  
public voidresetConfiguration()
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 null, sets their additivity flag to true and sets the level of the root logger to {@link Level#DEBUG DEBUG}. Moreover, message disabling is set its default "off" value.

Existing categories are not removed. They are just reset.

This method should be used sparingly and with care as it will block all logging until it is completed.

since
0.8.5


    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();
  
public voidsetDisableOverride(java.lang.String override)
Does mothing.

deprecated
Deprecated with no replacement.

    LogLog.warn("The Hiearchy.setDisableOverride method has been deprecated.");
  
public voidsetRenderer(java.lang.Class renderedClass, org.apache.log4j.or.ObjectRenderer renderer)
Used by subclasses to add a renderer to the hierarchy passed as parameter.

    rendererMap.put(renderedClass, renderer);
  
public voidsetThreshold(java.lang.String levelStr)
The string form of {@link #setThreshold(Level)}.

    Level l = (Level) Level.toLevel(levelStr, null);
    if(l != null) {
      setThreshold(l);
    } else {
      LogLog.warn("Could not convert ["+levelStr+"] to Level.");
    }
  
public voidsetThreshold(org.apache.log4j.Level l)
Enable logging for logging requests with level l or higher. By default all levels are enabled.

param
l The minimum level for which logging requests are sent to their appenders.

    if(l != null) {
      thresholdInt = l.level;
      threshold = l;
    }
  
public voidshutdown()
Shutting down a hierarchy will safely close and remove all appenders in all categories including the root logger.

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.

The shutdown 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

    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();
      }
    }
  
private final voidupdateChildren(org.apache.log4j.ProvisionNode pn, org.apache.log4j.Logger logger)
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.

    //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;
      }
    }
  
private final voidupdateParents(org.apache.log4j.Logger cat)
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.

    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;