Class AsyncAppender
- java.lang.Object
-
- org.apache.logging.log4j.core.AbstractLifeCycle
-
- org.apache.logging.log4j.core.filter.AbstractFilterable
-
- org.apache.logging.log4j.core.appender.AbstractAppender
-
- org.apache.logging.log4j.core.appender.AsyncAppender
-
- All Implemented Interfaces:
Appender,Filterable,LocationAware,LifeCycle,LifeCycle2
@Plugin(name="Async", category="Core", elementType="appender", printObject=true) public final class AsyncAppender extends AbstractAppender
Appends to one or more Appenders asynchronously. You can configure an AsyncAppender with one or more Appenders and an Appender to append to if the queue is full. The AsyncAppender does not allow a filter to be specified on the Appender references.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAsyncAppender.Builder<B extends AsyncAppender.Builder<B>>-
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
-
Field Summary
Fields Modifier and Type Field Description private AppenderRef[]appenderRefsprivate AsyncQueueFullPolicyasyncQueueFullPolicyprivate booleanblockingprivate Configurationconfigprivate static intDEFAULT_QUEUE_SIZEprivate AsyncAppenderEventDispatcherdispatcherprivate AppenderControlerrorAppenderprivate java.lang.StringerrorRefprivate booleanincludeLocationprivate java.util.concurrent.BlockingQueue<LogEvent>queueprivate intqueueSizeprivate longshutdownTimeout-
Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
-
Fields inherited from interface org.apache.logging.log4j.core.Appender
ELEMENT_TYPE, EMPTY_ARRAY
-
-
Constructor Summary
Constructors Modifier Constructor Description privateAsyncAppender(java.lang.String name, Filter filter, AppenderRef[] appenderRefs, java.lang.String errorRef, int queueSize, boolean blocking, boolean ignoreExceptions, long shutdownTimeout, Configuration config, boolean includeLocation, BlockingQueueFactory<LogEvent> blockingQueueFactory, Property[] properties)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidappend(LogEvent logEvent)Actual writing occurs here.static AsyncAppendercreateAppender(AppenderRef[] appenderRefs, java.lang.String errorRef, boolean blocking, long shutdownTimeout, int size, java.lang.String name, boolean includeLocation, Filter filter, Configuration config, boolean ignoreExceptions)Deprecated.useAsyncAppender.Builderinsteadjava.lang.String[]getAppenderRefStrings()Returns the names of the appenders that this asyncAppender delegates to as an array of Strings.java.util.List<Appender>getAppenders()Gets all Appenders.java.lang.StringgetErrorRef()Returns the name of the appender that any errors are logged to ornull.intgetQueueCapacity()intgetQueueRemainingCapacity()intgetQueueSize()Returns the number of elements in the queue.private booleanhandleInterruptedException(LogEvent memento)booleanisBlocking()Returnstrueif this AsyncAppender will block when the queue is full, orfalseif events are dropped when the queue is full.booleanisIncludeLocation()Returnstrueif this AsyncAppender will take a snapshot of the stack with every log event to determine the class and method where the logging call was made.voidlogMessageInBackgroundThread(LogEvent logEvent)FOR INTERNAL USE ONLY.voidlogMessageInCurrentThread(LogEvent logEvent)FOR INTERNAL USE ONLY.private voidlogToErrorAppenderIfNecessary(boolean appendSuccessful, LogEvent logEvent)static AsyncAppender.BuildernewBuilder()voidstart()Make the Filter available for use.booleanstop(long timeout, java.util.concurrent.TimeUnit timeUnit)Cleanup the Filter.private booleantransfer(LogEvent memento)-
Methods inherited from class org.apache.logging.log4j.core.appender.AbstractAppender
error, error, error, getHandler, getLayout, getName, ignoreExceptions, parseInt, requiresLocation, setHandler, toSerializable, toString
-
Methods inherited from class org.apache.logging.log4j.core.filter.AbstractFilterable
addFilter, getFilter, getPropertyArray, hasFilter, isFiltered, removeFilter, stop
-
Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop
-
-
-
-
Field Detail
-
DEFAULT_QUEUE_SIZE
private static final int DEFAULT_QUEUE_SIZE
- See Also:
- Constant Field Values
-
queue
private final java.util.concurrent.BlockingQueue<LogEvent> queue
-
queueSize
private final int queueSize
-
blocking
private final boolean blocking
-
shutdownTimeout
private final long shutdownTimeout
-
config
private final Configuration config
-
appenderRefs
private final AppenderRef[] appenderRefs
-
errorRef
private final java.lang.String errorRef
-
includeLocation
private final boolean includeLocation
-
errorAppender
private AppenderControl errorAppender
-
dispatcher
private AsyncAppenderEventDispatcher dispatcher
-
asyncQueueFullPolicy
private AsyncQueueFullPolicy asyncQueueFullPolicy
-
-
Constructor Detail
-
AsyncAppender
private AsyncAppender(java.lang.String name, Filter filter, AppenderRef[] appenderRefs, java.lang.String errorRef, int queueSize, boolean blocking, boolean ignoreExceptions, long shutdownTimeout, Configuration config, boolean includeLocation, BlockingQueueFactory<LogEvent> blockingQueueFactory, Property[] properties)
-
-
Method Detail
-
start
public void start()
Description copied from class:AbstractFilterableMake the Filter available for use.- Specified by:
startin interfaceLifeCycle- Overrides:
startin classAbstractFilterable
-
stop
public boolean stop(long timeout, java.util.concurrent.TimeUnit timeUnit)Description copied from class:AbstractFilterableCleanup the Filter.- Specified by:
stopin interfaceLifeCycle2- Overrides:
stopin classAbstractFilterable- Parameters:
timeout- the maximum time to waittimeUnit- the time unit of the timeout argument- Returns:
- true if the receiver was stopped cleanly and normally, false otherwise.
-
append
public void append(LogEvent logEvent)
Actual writing occurs here.- Parameters:
logEvent- The LogEvent.
-
transfer
private boolean transfer(LogEvent memento)
-
logMessageInCurrentThread
public void logMessageInCurrentThread(LogEvent logEvent)
FOR INTERNAL USE ONLY.- Parameters:
logEvent- the event to log
-
logMessageInBackgroundThread
public void logMessageInBackgroundThread(LogEvent logEvent)
FOR INTERNAL USE ONLY.- Parameters:
logEvent- the event to log
-
handleInterruptedException
private boolean handleInterruptedException(LogEvent memento)
-
logToErrorAppenderIfNecessary
private void logToErrorAppenderIfNecessary(boolean appendSuccessful, LogEvent logEvent)
-
createAppender
@Deprecated public static AsyncAppender createAppender(AppenderRef[] appenderRefs, java.lang.String errorRef, boolean blocking, long shutdownTimeout, int size, java.lang.String name, boolean includeLocation, Filter filter, Configuration config, boolean ignoreExceptions)
Deprecated.useAsyncAppender.BuilderinsteadCreate an AsyncAppender. This method is retained for backwards compatibility. New code should use theAsyncAppender.Builderinstead. This factory will useArrayBlockingQueueFactoryby default as was the behavior pre-2.7.- Parameters:
appenderRefs- The Appenders to reference.errorRef- An optional Appender to write to if the queue is full or other errors occur.blocking- True if the Appender should wait when the queue is full. The default is true.shutdownTimeout- How many milliseconds the Appender should wait to flush outstanding log events in the queue on shutdown. The default is zero which means to wait forever.size- The size of the event queue. The default is 128.name- The name of the Appender.includeLocation- whether to include location information. The default is false.filter- The Filter or null.config- The Configuration.ignoreExceptions- If"true"(default) exceptions encountered when appending events are logged; otherwise they are propagated to the caller.- Returns:
- The AsyncAppender.
-
newBuilder
@PluginBuilderFactory public static AsyncAppender.Builder newBuilder()
-
getAppenderRefStrings
public java.lang.String[] getAppenderRefStrings()
Returns the names of the appenders that this asyncAppender delegates to as an array of Strings.- Returns:
- the names of the sink appenders
-
isIncludeLocation
public boolean isIncludeLocation()
Returnstrueif this AsyncAppender will take a snapshot of the stack with every log event to determine the class and method where the logging call was made.- Returns:
trueif location is included with every event,falseotherwise
-
isBlocking
public boolean isBlocking()
Returnstrueif this AsyncAppender will block when the queue is full, orfalseif events are dropped when the queue is full.- Returns:
- whether this AsyncAppender will block or drop events when the queue is full.
-
getAppenders
public java.util.List<Appender> getAppenders()
Gets all Appenders.- Returns:
- a list of Appenders.
-
getErrorRef
public java.lang.String getErrorRef()
Returns the name of the appender that any errors are logged to ornull.- Returns:
- the name of the appender that any errors are logged to or
null
-
getQueueCapacity
public int getQueueCapacity()
-
getQueueRemainingCapacity
public int getQueueRemainingCapacity()
-
getQueueSize
public int getQueueSize()
Returns the number of elements in the queue.- Returns:
- the number of elements in the queue.
- Since:
- 2.11.1
-
-