Class AsyncLoggerConfigDisruptor
java.lang.Object
org.apache.logging.log4j.core.AbstractLifeCycle
org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor
- All Implemented Interfaces:
AsyncLoggerConfigDelegate
,LifeCycle
,LifeCycle2
public class AsyncLoggerConfigDisruptor
extends AbstractLifeCycle
implements AsyncLoggerConfigDelegate
Helper class decoupling the
AsyncLoggerConfig
class from the LMAX Disruptor library.
AsyncLoggerConfig
is a plugin, and will be loaded even if users do not configure any <asyncLogger>
or
<asyncRoot>
elements in the configuration. If AsyncLoggerConfig
has inner classes that extend or
implement classes from the Disruptor library, a NoClassDefFoundError
is thrown if the Disruptor jar is not in
the classpath when the PluginManager loads the AsyncLoggerConfig
plugin from the pre-defined plugins
definition file.
This class serves to make the dependency on the Disruptor optional, so that these classes are only loaded when the
AsyncLoggerConfig
is actually used.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
RingBuffer events contain all information necessary to perform the work in a separate thread.private static class
EventHandler performs the work in a separate thread.Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private AsyncQueueFullPolicy
private long
private com.lmax.disruptor.dsl.Disruptor
<AsyncLoggerConfigDisruptor.Log4jEventWrapper> private com.lmax.disruptor.EventFactory
<AsyncLoggerConfigDisruptor.Log4jEventWrapper> private static final com.lmax.disruptor.EventFactory
<AsyncLoggerConfigDisruptor.Log4jEventWrapper> Factory used to populate the RingBuffer with events.private static final int
private Boolean
private static final com.lmax.disruptor.EventFactory
<AsyncLoggerConfigDisruptor.Log4jEventWrapper> Factory used to populate the RingBuffer with events.private static final com.lmax.disruptor.EventTranslatorTwoArg
<AsyncLoggerConfigDisruptor.Log4jEventWrapper, LogEvent, AsyncLoggerConfig> Object responsible for passing on data to a RingBuffer event with a MutableLogEvent.private final Object
private int
private static final int
private com.lmax.disruptor.EventTranslatorTwoArg
<AsyncLoggerConfigDisruptor.Log4jEventWrapper, LogEvent, AsyncLoggerConfig> private static final com.lmax.disruptor.EventTranslatorTwoArg
<AsyncLoggerConfigDisruptor.Log4jEventWrapper, LogEvent, AsyncLoggerConfig> Object responsible for passing on data to a specific RingBuffer event.Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateRingBufferAdmin
(String contextName, String loggerConfigName) Creates and returns a newRingBufferAdmin
that instruments the ringbuffer of thisAsyncLoggerConfig
.private void
enqueue
(LogEvent logEvent, AsyncLoggerConfig asyncLoggerConfig) void
enqueueEvent
(LogEvent event, AsyncLoggerConfig asyncLoggerConfig) Enqueues theLogEvent
on the mixed configuration ringbuffer.private LogEvent
ensureImmutable
(LogEvent event) getEventRoute
(Level logLevel) Returns theEventRoute
for the event with the specified level.private static boolean
hasBacklog
(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor) Returnstrue
if the specified disruptor still has unprocessed events.private boolean
hasLog4jBeenShutDown
(com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper> aDisruptor) Returnstrue
if the specified disruptor is null.private LogEvent
prepareEvent
(LogEvent event) private int
void
setLogEventFactory
(LogEventFactory logEventFactory) Notifies the delegate what LogEventFactory an AsyncLoggerConfig is using, so the delegate can determine whether to populate the ring buffer with mutable log events or not.private void
void
start()
Increases the reference count and creates and starts a new Disruptor and associated thread if none currently exists.boolean
Decreases the reference count.private boolean
boolean
tryEnqueue
(LogEvent event, AsyncLoggerConfig asyncLoggerConfig) 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 Details
-
MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN- See Also:
-
SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS- See Also:
-
FACTORY
private static final com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper> FACTORYFactory used to populate the RingBuffer with events. These event objects are then re-used during the life of the RingBuffer. -
MUTABLE_FACTORY
private static final com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper> MUTABLE_FACTORYFactory used to populate the RingBuffer with events. These event objects are then re-used during the life of the RingBuffer. -
TRANSLATOR
private static final com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent, TRANSLATORAsyncLoggerConfig> Object responsible for passing on data to a specific RingBuffer event. -
MUTABLE_TRANSLATOR
private static final com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent, MUTABLE_TRANSLATORAsyncLoggerConfig> Object responsible for passing on data to a RingBuffer event with a MutableLogEvent. -
ringBufferSize
private int ringBufferSize -
asyncQueueFullPolicy
-
mutable
-
disruptor
private volatile com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper> disruptor -
backgroundThreadId
private long backgroundThreadId -
factory
-
translator
private com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent, translatorAsyncLoggerConfig> -
alreadyLoggedWarning
private volatile boolean alreadyLoggedWarning -
queueFullEnqueueLock
-
-
Constructor Details
-
AsyncLoggerConfigDisruptor
public AsyncLoggerConfigDisruptor()
-
-
Method Details
-
setLogEventFactory
Description copied from interface:AsyncLoggerConfigDelegate
Notifies the delegate what LogEventFactory an AsyncLoggerConfig is using, so the delegate can determine whether to populate the ring buffer with mutable log events or not. This method may be invoked multiple times for all AsyncLoggerConfigs that use this delegate.- Specified by:
setLogEventFactory
in interfaceAsyncLoggerConfigDelegate
- Parameters:
logEventFactory
- the factory used
-
start
public void start()Increases the reference count and creates and starts a new Disruptor and associated thread if none currently exists.- Specified by:
start
in interfaceLifeCycle
- Overrides:
start
in classAbstractLifeCycle
- See Also:
-
stop
Decreases the reference count. If the reference count reached zero, the Disruptor and its associated thread are shut down and their references set tonull
.- Specified by:
stop
in interfaceLifeCycle2
- Overrides:
stop
in classAbstractLifeCycle
- 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.
-
hasBacklog
private static boolean hasBacklog(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor) Returnstrue
if the specified disruptor still has unprocessed events. -
getEventRoute
Description copied from interface:AsyncLoggerConfigDelegate
Returns theEventRoute
for the event with the specified level.- Specified by:
getEventRoute
in interfaceAsyncLoggerConfigDelegate
- Parameters:
logLevel
- the level of the event to log- Returns:
- the
EventRoute
-
remainingDisruptorCapacity
private int remainingDisruptorCapacity() -
hasLog4jBeenShutDown
private boolean hasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper> aDisruptor) Returnstrue
if the specified disruptor is null. -
enqueueEvent
Description copied from interface:AsyncLoggerConfigDelegate
Enqueues theLogEvent
on the mixed configuration ringbuffer. This method must only be used afterAsyncLoggerConfigDelegate.tryEnqueue(LogEvent, AsyncLoggerConfig)
returnsfalse
indicating that the ringbuffer is full, otherwise it may incur unnecessary synchronization.- Specified by:
enqueueEvent
in interfaceAsyncLoggerConfigDelegate
-
prepareEvent
-
showWarningAboutCustomLogEventWithReusableMessage
-
enqueue
-
synchronizeEnqueueWhenQueueFull
private boolean synchronizeEnqueueWhenQueueFull() -
tryEnqueue
- Specified by:
tryEnqueue
in interfaceAsyncLoggerConfigDelegate
-
ensureImmutable
-
createRingBufferAdmin
Description copied from interface:AsyncLoggerConfigDelegate
Creates and returns a newRingBufferAdmin
that instruments the ringbuffer of thisAsyncLoggerConfig
.- Specified by:
createRingBufferAdmin
in interfaceAsyncLoggerConfigDelegate
- Parameters:
contextName
- name of theLoggerContext
loggerConfigName
- name of the logger config- Returns:
- the RingBufferAdmin that instruments the ringbuffer
-