Class BackTraceAppender

  • All Implemented Interfaces:
    org.apache.log4j.Appender, org.apache.log4j.spi.AppenderAttachable, org.apache.log4j.spi.OptionHandler

    public class BackTraceAppender
    extends org.apache.log4j.AppenderSkeleton
    implements org.apache.log4j.spi.AppenderAttachable
    For Log4j: Write debug messages only when an error occurs.

    This appender holds back debug messages in a buffer and writes them only out when an error occurs.

    For more flexibility, the BufferedAppender does not write to a file itself. It must be chained with another Appender to produce output.

    You should clear the buffer at the beginning or end of each HTTP request, to ensure that the buffer does not contain old stuff when processing the next HTTP request.

    Example log4j.xml

    Author:
    Stefan Frings, http://stefanfrings.de/javautils
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Vector<org.apache.log4j.Appender> appenderList
      The appenders used for output
      • Fields inherited from class org.apache.log4j.AppenderSkeleton

        closed, errorHandler, headFilter, layout, name, tailFilter, threshold
    • Constructor Summary

      Constructors 
      Constructor Description
      BackTraceAppender()
      Constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAppender​(org.apache.log4j.Appender newAppender)
      Attach an appender.
      void append​(org.apache.log4j.spi.LoggingEvent event)
      This method is called when a log message got produced.
      int appendLoopOnAppenders​(org.apache.log4j.spi.LoggingEvent event)
      Call the doAppend method on all attached appenders.
      static void clearBuffer()
      Clear the buffer(s) of the current thread.
      void close()
      Close this appender, release buffer memory.
      Enumeration getAllAppenders()
      Get all attached appenders as an Enumeration.
      org.apache.log4j.Appender getAppender​(String name)
      Look for an attached appender named as name.
      boolean isAttached​(org.apache.log4j.Appender appender)
      Returns true if the specified appender is in the list of attached appenders, false otherwise.
      void removeAllAppenders()
      Remove and close all previously attached appenders.
      void removeAppender​(String name)
      Remove the appender with the name passed as parameter form the list of appenders.
      void removeAppender​(org.apache.log4j.Appender appender)
      Remove the appender passed as parameter form the list of attached appenders.
      boolean requiresLayout()
      This appender does not require a layout because it uses another appender to format messages.
      void setAllowedLevel​(String level)  
      void setBufferSize​(int size)  
      void setTriggerLevel​(String level)  
      • Methods inherited from class org.apache.log4j.AppenderSkeleton

        activateOptions, addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
    • Field Detail

      • appenderList

        protected Vector<org.apache.log4j.Appender> appenderList
        The appenders used for output
    • Constructor Detail

      • BackTraceAppender

        public BackTraceAppender()
        Constructor
    • Method Detail

      • close

        public void close()
        Close this appender, release buffer memory.
        Specified by:
        close in interface org.apache.log4j.Appender
      • clearBuffer

        public static void clearBuffer()
        Clear the buffer(s) of the current thread.
      • append

        public void append​(org.apache.log4j.spi.LoggingEvent event)
        This method is called when a log message got produced. It buffers debug/trace messages until a warning/error occurs.
        Specified by:
        append in class org.apache.log4j.AppenderSkeleton
      • requiresLayout

        public boolean requiresLayout()
        This appender does not require a layout because it uses another appender to format messages.
        Specified by:
        requiresLayout in interface org.apache.log4j.Appender
        Returns:
        false
      • setTriggerLevel

        public void setTriggerLevel​(String level)
      • setAllowedLevel

        public void setAllowedLevel​(String level)
      • setBufferSize

        public void setBufferSize​(int size)
      • addAppender

        public void addAppender​(org.apache.log4j.Appender newAppender)
        Attach an appender. If the appender is already in the list in won't be added again.
        Specified by:
        addAppender in interface org.apache.log4j.spi.AppenderAttachable
      • appendLoopOnAppenders

        public int appendLoopOnAppenders​(org.apache.log4j.spi.LoggingEvent event)
        Call the doAppend method on all attached appenders.
      • getAllAppenders

        public Enumeration getAllAppenders()
        Get all attached appenders as an Enumeration. If there are no attached appenders null is returned.
        Specified by:
        getAllAppenders in interface org.apache.log4j.spi.AppenderAttachable
        Returns:
        Enumeration An enumeration of attached appenders.
      • getAppender

        public org.apache.log4j.Appender getAppender​(String name)
        Look for an attached appender named as name.

        Return the appender with that name if in the list. Return null otherwise.

        Specified by:
        getAppender in interface org.apache.log4j.spi.AppenderAttachable
      • isAttached

        public boolean isAttached​(org.apache.log4j.Appender appender)
        Returns true if the specified appender is in the list of attached appenders, false otherwise.
        Specified by:
        isAttached in interface org.apache.log4j.spi.AppenderAttachable
      • removeAllAppenders

        public void removeAllAppenders()
        Remove and close all previously attached appenders.
        Specified by:
        removeAllAppenders in interface org.apache.log4j.spi.AppenderAttachable
      • removeAppender

        public void removeAppender​(org.apache.log4j.Appender appender)
        Remove the appender passed as parameter form the list of attached appenders.
        Specified by:
        removeAppender in interface org.apache.log4j.spi.AppenderAttachable
      • removeAppender

        public void removeAppender​(String name)
        Remove the appender with the name passed as parameter form the list of appenders.
        Specified by:
        removeAppender in interface org.apache.log4j.spi.AppenderAttachable