Class BackTraceAppender

java.lang.Object
org.apache.log4j.AppenderSkeleton
de.stefanfrings.utils.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>
    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
    Constructor
  • Method Summary

    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)
    static void
    Clear the buffer(s) of the current thread.
    void
    Close this appender, release buffer memory.
    Get all attached appenders as an Enumeration.
    org.apache.log4j.Appender
    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
    Remove and close all previously attached appenders.
    void
    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
    This appender does not require a layout because it uses another appender to format messages.
    void
    Set the log level.
    void
    setBufferSize(int size)
    Set buffer size.
    void
    Set trigger 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

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • appenderList

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

    • BackTraceAppender

      public BackTraceAppender()
      Constructor
  • Method Details

    • 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)
      Set trigger level.
      Parameters:
      level - Log messages that trigger output of buffered messages: WARN or ERROR
    • setAllowedLevel

      public void setAllowedLevel(String level)
      Set the log level.
      Parameters:
      level - Log messages to output immediately: TRACE, DEBUG, INFO, WARN, ERROR or OFF
    • setBufferSize

      public void setBufferSize(int size)
      Set buffer size.
      Parameters:
      size - Maximum number of buffered messages for each thread
    • 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)
    • 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