Class BufferedAppender<E>

java.lang.Object
ch.qos.logback.core.spi.ContextAwareBase
ch.qos.logback.core.AppenderBase<E>
de.stefanfrings.utils.BufferedAppender<E>
All Implemented Interfaces:
ch.qos.logback.core.Appender<E>, ch.qos.logback.core.spi.AppenderAttachable<E>, ch.qos.logback.core.spi.ContextAware, ch.qos.logback.core.spi.FilterAttachable<E>, ch.qos.logback.core.spi.LifeCycle

public class BufferedAppender<E> extends ch.qos.logback.core.AppenderBase<E> implements ch.qos.logback.core.spi.AppenderAttachable<E>
For Logback: 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 logback.xml

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

    Fields inherited from class ch.qos.logback.core.AppenderBase

    name, started

    Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase

    context
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addAppender(ch.qos.logback.core.Appender<E> newAppender)
     
    protected void
    append(E event)
    This method is called when a log message got produced.
    int
    static void
    Discard buffered messages of the current thread.
    void
     
    boolean
    detachAppender(ch.qos.logback.core.Appender<E> appender)
     
    boolean
     
    ch.qos.logback.core.Appender<E>
     
    boolean
    isAttached(ch.qos.logback.core.Appender<E> appender)
     
    Iterator<ch.qos.logback.core.Appender<E>>
     
    void
    void
    setBufferSize(int size)
    Set buffer size.
    void

    Methods inherited from class ch.qos.logback.core.AppenderBase

    addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, start, stop, toString

    Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase

    addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface ch.qos.logback.core.spi.ContextAware

    addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
  • Constructor Details

    • BufferedAppender

      public BufferedAppender()
      Constructor
  • Method Details

    • clearBuffer

      public static void clearBuffer()
      Discard buffered messages of the current thread.
    • append

      protected void append(E 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 ch.qos.logback.core.AppenderBase<E>
    • setAllowedLevel

      public void setAllowedLevel(String s)
    • setTriggerLevel

      public void setTriggerLevel(String s)
    • setBufferSize

      public void setBufferSize(int size)
      Set buffer size.
      Parameters:
      size - Maximum number of buffered messages for each thread
    • addAppender

      public void addAppender(ch.qos.logback.core.Appender<E> newAppender)
      Specified by:
      addAppender in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • appendLoopOnAppenders

      public int appendLoopOnAppenders(E e)
    • iteratorForAppenders

      public Iterator<ch.qos.logback.core.Appender<E>> iteratorForAppenders()
      Specified by:
      iteratorForAppenders in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • getAppender

      public ch.qos.logback.core.Appender<E> getAppender(String name)
      Specified by:
      getAppender in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • isAttached

      public boolean isAttached(ch.qos.logback.core.Appender<E> appender)
      Specified by:
      isAttached in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • detachAndStopAllAppenders

      public void detachAndStopAllAppenders()
      Specified by:
      detachAndStopAllAppenders in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • detachAppender

      public boolean detachAppender(ch.qos.logback.core.Appender<E> appender)
      Specified by:
      detachAppender in interface ch.qos.logback.core.spi.AppenderAttachable<E>
    • detachAppender

      public boolean detachAppender(String name)
      Specified by:
      detachAppender in interface ch.qos.logback.core.spi.AppenderAttachable<E>