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 theAsyncLoggerConfigclass from the LMAX Disruptor library.AsyncLoggerConfigis a plugin, and will be loaded even if users do not configure any<asyncLogger>or<asyncRoot>elements in the configuration. IfAsyncLoggerConfighas inner classes that extend or implement classes from the Disruptor library, aNoClassDefFoundErroris thrown if the Disruptor jar is not in the classpath when the PluginManager loads theAsyncLoggerConfigplugin 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
AsyncLoggerConfigis actually used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAsyncLoggerConfigDisruptor.Log4jEventWrapperRingBuffer events contain all information necessary to perform the work in a separate thread.private static classAsyncLoggerConfigDisruptor.Log4jEventWrapperHandlerEventHandler performs the work in a separate thread.-
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
-
Field Summary
Fields Modifier and Type Field Description private booleanalreadyLoggedWarningprivate AsyncQueueFullPolicyasyncQueueFullPolicyprivate AsyncWaitStrategyFactoryasyncWaitStrategyFactoryprivate longbackgroundThreadIdprivate com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper>disruptorprivate com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper>factoryprivate static com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper>FACTORYFactory used to populate the RingBuffer with events.private static intMAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWNprivate java.lang.Booleanmutableprivate static com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper>MUTABLE_FACTORYFactory used to populate the RingBuffer with events.private static com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent,AsyncLoggerConfig>MUTABLE_TRANSLATORObject responsible for passing on data to a RingBuffer event with a MutableLogEvent.private java.lang.ObjectqueueFullEnqueueLockprivate intringBufferSizeprivate static intSLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTSprivate com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent,AsyncLoggerConfig>translatorprivate static com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent,AsyncLoggerConfig>TRANSLATORObject responsible for passing on data to a specific RingBuffer event.private com.lmax.disruptor.WaitStrategywaitStrategy-
Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
-
-
Constructor Summary
Constructors Constructor Description AsyncLoggerConfigDisruptor(AsyncWaitStrategyFactory asyncWaitStrategyFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RingBufferAdmincreateRingBufferAdmin(java.lang.String contextName, java.lang.String loggerConfigName)Creates and returns a newRingBufferAdminthat instruments the ringbuffer of thisAsyncLoggerConfig.private voidenqueue(LogEvent logEvent, AsyncLoggerConfig asyncLoggerConfig)voidenqueueEvent(LogEvent event, AsyncLoggerConfig asyncLoggerConfig)Enqueues theLogEventon the mixed configuration ringbuffer.private LogEventensureImmutable(LogEvent event)EventRoutegetEventRoute(Level logLevel)Returns theEventRoutefor the event with the specified level.(package private) com.lmax.disruptor.WaitStrategygetWaitStrategy()private static booleanhasBacklog(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor)Returnstrueif the specified disruptor still has unprocessed events.private booleanhasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper> aDisruptor)Returnstrueif the specified disruptor is null.private LogEventprepareEvent(LogEvent event)private intremainingDisruptorCapacity()voidsetLogEventFactory(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 voidshowWarningAboutCustomLogEventWithReusableMessage(LogEvent logEvent)voidstart()Increases the reference count and creates and starts a new Disruptor and associated thread if none currently exists.booleanstop(long timeout, java.util.concurrent.TimeUnit timeUnit)Decreases the reference count.private booleansynchronizeEnqueueWhenQueueFull()booleantryEnqueue(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 Detail
-
MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
- See Also:
- Constant Field Values
-
SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
- See Also:
- Constant Field Values
-
FACTORY
private static final com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper> FACTORY
Factory 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_FACTORY
Factory 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,AsyncLoggerConfig> TRANSLATOR
Object responsible for passing on data to a specific RingBuffer event.
-
MUTABLE_TRANSLATOR
private static final com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent,AsyncLoggerConfig> MUTABLE_TRANSLATOR
Object responsible for passing on data to a RingBuffer event with a MutableLogEvent.
-
ringBufferSize
private int ringBufferSize
-
asyncQueueFullPolicy
private AsyncQueueFullPolicy asyncQueueFullPolicy
-
mutable
private java.lang.Boolean mutable
-
disruptor
private volatile com.lmax.disruptor.dsl.Disruptor<AsyncLoggerConfigDisruptor.Log4jEventWrapper> disruptor
-
backgroundThreadId
private long backgroundThreadId
-
factory
private com.lmax.disruptor.EventFactory<AsyncLoggerConfigDisruptor.Log4jEventWrapper> factory
-
translator
private com.lmax.disruptor.EventTranslatorTwoArg<AsyncLoggerConfigDisruptor.Log4jEventWrapper,LogEvent,AsyncLoggerConfig> translator
-
alreadyLoggedWarning
private volatile boolean alreadyLoggedWarning
-
asyncWaitStrategyFactory
private final AsyncWaitStrategyFactory asyncWaitStrategyFactory
-
waitStrategy
private com.lmax.disruptor.WaitStrategy waitStrategy
-
queueFullEnqueueLock
private final java.lang.Object queueFullEnqueueLock
-
-
Constructor Detail
-
AsyncLoggerConfigDisruptor
public AsyncLoggerConfigDisruptor(AsyncWaitStrategyFactory asyncWaitStrategyFactory)
-
-
Method Detail
-
getWaitStrategy
com.lmax.disruptor.WaitStrategy getWaitStrategy()
-
setLogEventFactory
public void setLogEventFactory(LogEventFactory logEventFactory)
Description copied from interface:AsyncLoggerConfigDelegateNotifies 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:
setLogEventFactoryin 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:
startin interfaceLifeCycle- Overrides:
startin classAbstractLifeCycle- See Also:
AbstractLifeCycle.stop()
-
stop
public boolean stop(long timeout, java.util.concurrent.TimeUnit timeUnit)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:
stopin interfaceLifeCycle2- Overrides:
stopin 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)
Returnstrueif the specified disruptor still has unprocessed events.
-
getEventRoute
public EventRoute getEventRoute(Level logLevel)
Description copied from interface:AsyncLoggerConfigDelegateReturns theEventRoutefor the event with the specified level.- Specified by:
getEventRoutein 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)
Returnstrueif the specified disruptor is null.
-
enqueueEvent
public void enqueueEvent(LogEvent event, AsyncLoggerConfig asyncLoggerConfig)
Description copied from interface:AsyncLoggerConfigDelegateEnqueues theLogEventon the mixed configuration ringbuffer. This method must only be used afterAsyncLoggerConfigDelegate.tryEnqueue(LogEvent, AsyncLoggerConfig)returnsfalseindicating that the ringbuffer is full, otherwise it may incur unnecessary synchronization.- Specified by:
enqueueEventin interfaceAsyncLoggerConfigDelegate
-
showWarningAboutCustomLogEventWithReusableMessage
private void showWarningAboutCustomLogEventWithReusableMessage(LogEvent logEvent)
-
enqueue
private void enqueue(LogEvent logEvent, AsyncLoggerConfig asyncLoggerConfig)
-
synchronizeEnqueueWhenQueueFull
private boolean synchronizeEnqueueWhenQueueFull()
-
tryEnqueue
public boolean tryEnqueue(LogEvent event, AsyncLoggerConfig asyncLoggerConfig)
- Specified by:
tryEnqueuein interfaceAsyncLoggerConfigDelegate
-
createRingBufferAdmin
public RingBufferAdmin createRingBufferAdmin(java.lang.String contextName, java.lang.String loggerConfigName)
Description copied from interface:AsyncLoggerConfigDelegateCreates and returns a newRingBufferAdminthat instruments the ringbuffer of thisAsyncLoggerConfig.- Specified by:
createRingBufferAdminin interfaceAsyncLoggerConfigDelegate- Parameters:
contextName- name of theLoggerContextloggerConfigName- name of the logger config- Returns:
- the RingBufferAdmin that instruments the ringbuffer
-
-