Class Hierarchy

java.lang.Object
org.apache.log4j.Hierarchy
All Implemented Interfaces:
LoggerRepository, RendererSupport, ThrowableRendererSupport

public class Hierarchy extends Object implements LoggerRepository, RendererSupport, ThrowableRendererSupport
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 getLogger(java.lang.String) 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.

  • Field Details

  • Constructor Details

    • Hierarchy

      public Hierarchy(Logger root)
      Creates a new logger hierarchy.
      Parameters:
      root - The root of the new hierarchy.
  • Method Details

    • getContext

      static LoggerContext getContext()
    • getInstance

      static Logger getInstance(LoggerContext context, String name)
    • getInstance

      static Logger getInstance(LoggerContext context, String name, LoggerFactory factory)
    • getInstance

      static Logger getInstance(LoggerContext context, String name, Hierarchy.PrivateLoggerAdapter factory)
    • getLoggersMap

      static ConcurrentMap<String,Logger> getLoggersMap(LoggerContext context)
    • getRootLogger

      static Logger getRootLogger(LoggerContext context)
    • addHierarchyEventListener

      public void addHierarchyEventListener(HierarchyEventListener listener)
      Description copied from interface: LoggerRepository
      Add a HierarchyEventListener event to the repository.
      Specified by:
      addHierarchyEventListener in interface LoggerRepository
      Parameters:
      listener - The listener
    • addRenderer

      public void addRenderer(Class classToRender, ObjectRenderer or)
      Adds an object renderer for a specific class.
    • clear

      public void clear()
      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
    • emitNoAppenderWarning

      public void emitNoAppenderWarning(Category cat)
      Specified by:
      emitNoAppenderWarning in interface LoggerRepository
    • exists

      public Logger exists(String name)
      Tests if the named logger exists in the hierarchy. If so return its reference, otherwise returns null.
      Specified by:
      exists in interface LoggerRepository
      Parameters:
      name - The name of the logger to search for.
    • exists

      Logger exists(String name, ClassLoader classLoader)
    • exists

      Logger exists(String name, LoggerContext loggerContext)
    • fireAddAppenderEvent

      public void fireAddAppenderEvent(Category logger, Appender appender)
      Specified by:
      fireAddAppenderEvent in interface LoggerRepository
    • fireRemoveAppenderEvent

      void fireRemoveAppenderEvent(Category logger, Appender appender)
    • getContext

      LoggerContext getContext(ClassLoader classLoader)
    • getCurrentCategories

      @Deprecated public Enumeration getCurrentCategories()
      Deprecated.
      Please use getCurrentLoggers() instead.
      Description copied from interface: LoggerRepository
      Deprecated. Please use LoggerRepository.getCurrentLoggers() instead.
      Specified by:
      getCurrentCategories in interface LoggerRepository
      Returns:
      an enumeration of loggers.
    • getCurrentLoggers

      public Enumeration getCurrentLoggers()
      Gets all the currently defined categories in this hierarchy as an Enumeration.

      The root logger is not included in the returned Enumeration.

      Specified by:
      getCurrentLoggers in interface LoggerRepository
    • getLogger

      public Logger getLogger(String name)
      Gets 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.

      Specified by:
      getLogger in interface LoggerRepository
      Parameters:
      name - The name of the logger to retrieve.
    • getLogger

      Logger getLogger(String name, ClassLoader classLoader)
    • getLogger

      public Logger getLogger(String name, LoggerFactory factory)
      Gets 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.

      Specified by:
      getLogger in interface LoggerRepository
      Parameters:
      name - The name of the logger to retrieve.
      factory - The factory that will make the new logger instance.
    • getLogger

      Logger getLogger(String name, LoggerFactory factory, ClassLoader classLoader)
    • getRendererMap

      public RendererMap getRendererMap()
      Gets the renderer map for this hierarchy.
      Specified by:
      getRendererMap in interface RendererSupport
    • getRootLogger

      public Logger getRootLogger()
      Gets the root of this hierarchy.
      Specified by:
      getRootLogger in interface LoggerRepository
      Since:
      0.9.0
    • getRootLogger

      Logger getRootLogger(ClassLoader classLoader)
    • getThreshold

      public Level getThreshold()
      Gets a Level representation of the enable state.
      Specified by:
      getThreshold in interface LoggerRepository
      Returns:
      the level.
      Since:
      1.2
    • getThrowableRenderer

      public ThrowableRenderer getThrowableRenderer()
      Get throwable renderer.
      Specified by:
      getThrowableRenderer in interface ThrowableRendererSupport
      Returns:
      throwable renderer, may be null.
    • isDisabled

      public boolean isDisabled(int level)
      This method will return true if this repository is disabled for level object passed as parameter and false otherwise. See also the threshold emthod.
      Specified by:
      isDisabled in interface LoggerRepository
      Parameters:
      level - The level
      Returns:
      whether this repository is disabled.
    • overrideAsNeeded

      @Deprecated public void overrideAsNeeded(String override)
      Deprecated.
      Deprecated with no replacement.
    • resetConfiguration

      public void resetConfiguration()
      Resets 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 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.

      Specified by:
      resetConfiguration in interface LoggerRepository
      Since:
      0.8.5
    • resetConfiguration

      void resetConfiguration(ClassLoader classLoader)
    • resetConfiguration

      void resetConfiguration(LoggerContext loggerContext)
    • setDisableOverride

      @Deprecated public void setDisableOverride(String override)
      Deprecated.
      Deprecated with no replacement.
      Does nothing.
    • setRenderer

      public void setRenderer(Class renderedClass, ObjectRenderer renderer)
      Used by subclasses to add a renderer to the hierarchy passed as parameter.
      Specified by:
      setRenderer in interface RendererSupport
    • setThreshold

      public void setThreshold(Level level)
      Enable logging for logging requests with level l or higher. By default all levels are enabled.
      Specified by:
      setThreshold in interface LoggerRepository
      Parameters:
      level - The minimum level for which logging requests are sent to their appenders.
    • setThreshold

      public void setThreshold(String levelStr)
      The string form of setThreshold(Level).
      Specified by:
      setThreshold in interface LoggerRepository
      Parameters:
      levelStr - The threshold value
    • setThrowableRenderer

      public void setThrowableRenderer(ThrowableRenderer throwableRenderer)
      Set throwable renderer.
      Specified by:
      setThrowableRenderer in interface ThrowableRendererSupport
      Parameters:
      throwableRenderer - renderer, may be null.
    • shutdown

      public void shutdown()
      Shutting down a hierarchy will safely close and remove all appenders in all categories including the root logger.

      Some appenders such as org.apache.log4j.net.SocketAppender and 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.

      Specified by:
      shutdown in interface LoggerRepository
      Since:
      1.0
    • shutdown

      public void shutdown(ClassLoader classLoader)
    • shutdown

      void shutdown(LoggerContext context)
    • updateChildren

      private final void updateChildren(ProvisionNode pn, 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.
    • updateParents

      private final void updateParents(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.