Package ch.qos.logback.core
Class AsyncAppenderBase<E>
- java.lang.Object
-
- ch.qos.logback.core.spi.ContextAwareBase
-
- ch.qos.logback.core.UnsynchronizedAppenderBase<E>
-
- ch.qos.logback.core.AsyncAppenderBase<E>
-
- Type Parameters:
E
-
- All Implemented Interfaces:
Appender<E>
,AppenderAttachable<E>
,ContextAware
,FilterAttachable<E>
,LifeCycle
- Direct Known Subclasses:
AsyncAppender
public class AsyncAppenderBase<E> extends UnsynchronizedAppenderBase<E> implements AppenderAttachable<E>
This appender and derived classes, log events asynchronously. In order to avoid loss of logging events, this appender should be closed. It is the user's responsibility to close appenders, typically at the end of the application lifecycle. This appender buffers events in aBlockingQueue
.AsyncAppenderBase.Worker
thread created by this appender takes events from the head of the queue, and dispatches them to the single appender attached to this appender.Please refer to the logback manual for further information about this appender.
- Since:
- 1.0.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
AsyncAppenderBase.Worker
-
Field Summary
Fields Modifier and Type Field Description (package private) AppenderAttachableImpl<E>
aai
(package private) int
appenderCount
(package private) java.util.concurrent.BlockingQueue<E>
blockingQueue
static int
DEFAULT_MAX_FLUSH_TIME
The default maximum queue flush time allowed during appender stop.static int
DEFAULT_QUEUE_SIZE
The default buffer size.(package private) int
discardingThreshold
(package private) int
maxFlushTime
(package private) boolean
neverBlock
(package private) int
queueSize
(package private) static int
UNDEFINED
(package private) AsyncAppenderBase.Worker
worker
-
Fields inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
ALLOWED_REPEATS, name, started
-
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
-
Constructor Summary
Constructors Constructor Description AsyncAppenderBase()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAppender(Appender<E> newAppender)
Add an appender.protected void
append(E eventObject)
void
detachAndStopAllAppenders()
Detach and processPriorToRemoval all previously added appenders.boolean
detachAppender(Appender<E> eAppender)
Detach the appender passed as parameter from the list of appenders.boolean
detachAppender(java.lang.String name)
Detach the appender with the name passed as parameter from the list of appenders.Appender<E>
getAppender(java.lang.String name)
Get an appender by name.int
getDiscardingThreshold()
int
getMaxFlushTime()
int
getNumberOfElementsInQueue()
Returns the number of elements currently in the blocking queue.int
getQueueSize()
int
getRemainingCapacity()
The remaining capacity available in the blocking queue.boolean
isAttached(Appender<E> eAppender)
Returnstrue
if the specified appender is in list of attached attached,false
otherwise.protected boolean
isDiscardable(E eventObject)
Is the eventObject passed as parameter discardable? The base class's implementation of this method always returns 'false' but sub-classes may (and do) override this method.boolean
isNeverBlock()
private boolean
isQueueBelowDiscardingThreshold()
java.util.Iterator<Appender<E>>
iteratorForAppenders()
Get an iterator for appenders contained in the parent object.protected void
preprocess(E eventObject)
Pre-process the event prior to queueing.private void
put(E eventObject)
private void
putUninterruptibly(E eventObject)
void
setDiscardingThreshold(int discardingThreshold)
void
setMaxFlushTime(int maxFlushTime)
void
setNeverBlock(boolean neverBlock)
void
setQueueSize(int queueSize)
void
start()
void
stop()
-
Methods inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, 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
-
-
-
-
Field Detail
-
aai
AppenderAttachableImpl<E> aai
-
blockingQueue
java.util.concurrent.BlockingQueue<E> blockingQueue
-
DEFAULT_QUEUE_SIZE
public static final int DEFAULT_QUEUE_SIZE
The default buffer size.- See Also:
- Constant Field Values
-
queueSize
int queueSize
-
appenderCount
int appenderCount
-
UNDEFINED
static final int UNDEFINED
- See Also:
- Constant Field Values
-
discardingThreshold
int discardingThreshold
-
neverBlock
boolean neverBlock
-
worker
AsyncAppenderBase.Worker worker
-
DEFAULT_MAX_FLUSH_TIME
public static final int DEFAULT_MAX_FLUSH_TIME
The default maximum queue flush time allowed during appender stop. If the worker takes longer than this time it will exit, discarding any remaining items in the queue- See Also:
- Constant Field Values
-
maxFlushTime
int maxFlushTime
-
-
Method Detail
-
isDiscardable
protected boolean isDiscardable(E eventObject)
Is the eventObject passed as parameter discardable? The base class's implementation of this method always returns 'false' but sub-classes may (and do) override this method.Note that only if the buffer is nearly full are events discarded. Otherwise, when the buffer is "not full" all events are logged.
- Parameters:
eventObject
-- Returns:
- - true if the event can be discarded, false otherwise
-
preprocess
protected void preprocess(E eventObject)
Pre-process the event prior to queueing. The base class does no pre-processing but sub-classes can override this behavior.- Parameters:
eventObject
-
-
start
public void start()
- Specified by:
start
in interfaceLifeCycle
- Overrides:
start
in classUnsynchronizedAppenderBase<E>
-
stop
public void stop()
- Specified by:
stop
in interfaceLifeCycle
- Overrides:
stop
in classUnsynchronizedAppenderBase<E>
-
append
protected void append(E eventObject)
- Specified by:
append
in classUnsynchronizedAppenderBase<E>
-
isQueueBelowDiscardingThreshold
private boolean isQueueBelowDiscardingThreshold()
-
put
private void put(E eventObject)
-
putUninterruptibly
private void putUninterruptibly(E eventObject)
-
getQueueSize
public int getQueueSize()
-
setQueueSize
public void setQueueSize(int queueSize)
-
getDiscardingThreshold
public int getDiscardingThreshold()
-
setDiscardingThreshold
public void setDiscardingThreshold(int discardingThreshold)
-
getMaxFlushTime
public int getMaxFlushTime()
-
setMaxFlushTime
public void setMaxFlushTime(int maxFlushTime)
-
getNumberOfElementsInQueue
public int getNumberOfElementsInQueue()
Returns the number of elements currently in the blocking queue.- Returns:
- number of elements currently in the queue.
-
setNeverBlock
public void setNeverBlock(boolean neverBlock)
-
isNeverBlock
public boolean isNeverBlock()
-
getRemainingCapacity
public int getRemainingCapacity()
The remaining capacity available in the blocking queue.- Returns:
- the remaining capacity
-
addAppender
public void addAppender(Appender<E> newAppender)
Description copied from interface:AppenderAttachable
Add an appender.- Specified by:
addAppender
in interfaceAppenderAttachable<E>
-
iteratorForAppenders
public java.util.Iterator<Appender<E>> iteratorForAppenders()
Description copied from interface:AppenderAttachable
Get an iterator for appenders contained in the parent object.- Specified by:
iteratorForAppenders
in interfaceAppenderAttachable<E>
-
getAppender
public Appender<E> getAppender(java.lang.String name)
Description copied from interface:AppenderAttachable
Get an appender by name.- Specified by:
getAppender
in interfaceAppenderAttachable<E>
-
isAttached
public boolean isAttached(Appender<E> eAppender)
Description copied from interface:AppenderAttachable
Returnstrue
if the specified appender is in list of attached attached,false
otherwise.- Specified by:
isAttached
in interfaceAppenderAttachable<E>
-
detachAndStopAllAppenders
public void detachAndStopAllAppenders()
Description copied from interface:AppenderAttachable
Detach and processPriorToRemoval all previously added appenders.- Specified by:
detachAndStopAllAppenders
in interfaceAppenderAttachable<E>
-
detachAppender
public boolean detachAppender(Appender<E> eAppender)
Description copied from interface:AppenderAttachable
Detach the appender passed as parameter from the list of appenders.- Specified by:
detachAppender
in interfaceAppenderAttachable<E>
-
detachAppender
public boolean detachAppender(java.lang.String name)
Description copied from interface:AppenderAttachable
Detach the appender with the name passed as parameter from the list of appenders.- Specified by:
detachAppender
in interfaceAppenderAttachable<E>
-
-