Package de.stefanfrings.utils
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.
- Author:
- Stefan Frings, http://stefanfrings.de/javautils
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Vector<org.apache.log4j.Appender>The appenders used for outputFields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAppender(org.apache.log4j.Appender newAppender) Attach an appender.voidappend(org.apache.log4j.spi.LoggingEvent event) This method is called when a log message got produced.intappendLoopOnAppenders(org.apache.log4j.spi.LoggingEvent event) static voidClear the buffer(s) of the current thread.voidclose()Close this appender, release buffer memory.Get all attached appenders as an Enumeration.org.apache.log4j.AppendergetAppender(String name) Look for an attached appender named asname.booleanisAttached(org.apache.log4j.Appender appender) Returnstrueif the specified appender is in the list of attached appenders,falseotherwise.voidRemove and close all previously attached appenders.voidremoveAppender(String name) Remove the appender with the name passed as parameter form the list of appenders.voidremoveAppender(org.apache.log4j.Appender appender) Remove the appender passed as parameter form the list of attached appenders.booleanThis appender does not require a layout because it uses another appender to format messages.voidsetAllowedLevel(String level) Set the log level.voidsetBufferSize(int size) Set buffer size.voidsetTriggerLevel(String level) 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
-
Field Details
-
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:
closein interfaceorg.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:
appendin classorg.apache.log4j.AppenderSkeleton
-
requiresLayout
public boolean requiresLayout()This appender does not require a layout because it uses another appender to format messages.- Specified by:
requiresLayoutin interfaceorg.apache.log4j.Appender- Returns:
- false
-
setTriggerLevel
Set trigger level.- Parameters:
level- Log messages that trigger output of buffered messages: WARN or ERROR
-
setAllowedLevel
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:
addAppenderin interfaceorg.apache.log4j.spi.AppenderAttachable
-
appendLoopOnAppenders
public int appendLoopOnAppenders(org.apache.log4j.spi.LoggingEvent event) -
getAllAppenders
Get all attached appenders as an Enumeration. If there are no attached appendersnullis returned.- Specified by:
getAllAppendersin interfaceorg.apache.log4j.spi.AppenderAttachable- Returns:
- Enumeration An enumeration of attached appenders.
-
getAppender
Look for an attached appender named asname.Return the appender with that name if in the list. Return null otherwise.
- Specified by:
getAppenderin interfaceorg.apache.log4j.spi.AppenderAttachable
-
isAttached
public boolean isAttached(org.apache.log4j.Appender appender) Returnstrueif the specified appender is in the list of attached appenders,falseotherwise.- Specified by:
isAttachedin interfaceorg.apache.log4j.spi.AppenderAttachable
-
removeAllAppenders
public void removeAllAppenders()Remove and close all previously attached appenders.- Specified by:
removeAllAppendersin interfaceorg.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:
removeAppenderin interfaceorg.apache.log4j.spi.AppenderAttachable
-
removeAppender
Remove the appender with the name passed as parameter form the list of appenders.- Specified by:
removeAppenderin interfaceorg.apache.log4j.spi.AppenderAttachable
-