Class AsyncLogger
- All Implemented Interfaces:
com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent>
,Serializable
,Logger
,ExtendedLogger
,LocationAwareLogger
,Supplier<LoggerConfig>
To use AsyncLogger, specify the System property
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
before you obtain a
Logger, and all Loggers returned by LogManager.getLogger will be AsyncLoggers.
Note that for performance reasons, this logger does not include source location by default. You need to specify
includeLocation="true"
in the configuration or any %class, %location or %line conversion patterns in your
log4j.xml configuration will produce either a "?" character or no output at all.
For best performance, use AsyncLogger with the RandomAccessFileAppender or RollingRandomAccessFileAppender, with immediateFlush=false. These appenders have built-in support for the batching mechanism used by the Disruptor library, and they will flush to disk at the end of each batch. This means that even with immediateFlush=false, there will never be any items left in the buffer; all log events will all be written to disk in a very efficient manner.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.apache.logging.log4j.core.Logger
Logger.LoggerProxy, Logger.PrivateConfig
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Clock
private static final ContextDataInjector
private boolean
private static final StatusLogger
private final AsyncLoggerDisruptor
private NanoClock
private static final ThreadNameCachingStrategy
private final ThreadLocal<RingBufferLogEventTranslator>
private final AsyncLogger.TranslatorType
private final AsyncLogger.TranslatorType
Fields inherited from class org.apache.logging.log4j.core.Logger
privateConfig
Fields inherited from class org.apache.logging.log4j.spi.AbstractLogger
CATCHING_MARKER, DEFAULT_FLOW_MESSAGE_FACTORY_CLASS, DEFAULT_MESSAGE_FACTORY_CLASS, ENTRY_MARKER, EXCEPTION_MARKER, EXIT_MARKER, FLOW_MARKER, logBuilder, name, THROWING_MARKER
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncLogger
(LoggerContext context, String name, MessageFactory messageFactory, AsyncLoggerDisruptor loggerDisruptor) Constructs anAsyncLogger
with the specified context, name and message factory. -
Method Summary
Modifier and TypeMethodDescriptionvoid
actualAsyncLog
(RingBufferLogEvent event) This method is called by the EventHandler that processes the RingBufferLogEvent in a separate thread.private StackTraceElement
Returns the caller location if requested,null
otherwise.private RingBufferLogEventTranslator
private static StringMap
getContextData
(RingBufferLogEvent event) (package private) NanoClock
private AsyncLogger.TranslatorType
private void
handleRingBufferFull
(StackTraceElement location, String fqcn, Level level, Marker marker, Message msg, Throwable thrown) private void
handleRingBufferFull
(RingBufferLogEventTranslator translator) private void
initTranslator
(RingBufferLogEventTranslator translator, String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) private void
initTranslator
(RingBufferLogEventTranslator translator, String fqcn, Level level, Marker marker, Message message, Throwable thrown) private void
private boolean
void
log
(Level level, Marker marker, String fqcn, StackTraceElement location, Message message, Throwable throwable) void
Logs a message at the specified level.(package private) void
logMessageInCurrentThread
(String fqcn, Level level, Marker marker, Message message, Throwable thrown) LOG4J2-471: prevent deadlock when RingBuffer is full and object being logged calls Logger.log() from its toString() methodprivate void
logWithThreadLocalTranslator
(String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.private void
logWithThreadLocalTranslator
(String fqcn, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.private void
logWithVarargTranslator
(String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.private void
Enqueues the specified log event data for logging in a background thread.private void
onPropertiesPresent
(RingBufferLogEvent event, List<Property> properties) private void
publish
(RingBufferLogEventTranslator translator) void
translateTo
(RingBufferLogEvent event, long sequence, Object... args) protected void
updateConfiguration
(Configuration newConfig) Associates this Logger with a new Configuration.Methods inherited from class org.apache.logging.log4j.core.Logger
addAppender, addFilter, equals, filterCount, get, getAppenders, getContext, getFilters, getLevel, getParent, hashCode, isAdditive, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, isEnabled, removeAppender, requiresLocation, setAdditive, setLevel, toString, writeReplace
Methods inherited from class org.apache.logging.log4j.spi.AbstractLogger
always, atDebug, atError, atFatal, atInfo, atLevel, atTrace, atWarn, catching, catching, catching, catchingMsg, checkMessageFactory, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, enter, enter, enter, enter, enter, entry, entry, entry, entryMsg, entryMsg, entryMsg, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, exit, exit, exit, exit, exitMsg, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, getMessageFactory, getName, getRecursionDepth, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, isDebugEnabled, isDebugEnabled, isEnabled, isEnabled, isErrorEnabled, isErrorEnabled, isFatalEnabled, isFatalEnabled, isInfoEnabled, isInfoEnabled, isTraceEnabled, isTraceEnabled, isWarnEnabled, isWarnEnabled, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, printf, printf, throwing, throwing, throwing, throwingMsg, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, traceEntry, traceEntry, traceEntry, traceEntry, traceEntry, traceExit, traceExit, traceExit, traceExit, traceExit, traceExit, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn
-
Field Details
-
LOGGER
-
CLOCK
-
CONTEXT_DATA_INJECTOR
-
THREAD_NAME_CACHING_STRATEGY
-
threadLocalTranslator
-
loggerDisruptor
-
includeLocation
private volatile boolean includeLocation -
nanoClock
-
threadLocalTranslatorType
-
varargTranslatorType
-
-
Constructor Details
-
AsyncLogger
public AsyncLogger(LoggerContext context, String name, MessageFactory messageFactory, AsyncLoggerDisruptor loggerDisruptor) Constructs anAsyncLogger
with the specified context, name and message factory.- Parameters:
context
- context of this loggername
- name of this loggermessageFactory
- message factory of this loggerloggerDisruptor
- helper class that logging can be delegated to. This object owns the Disruptor.
-
-
Method Details
-
updateConfiguration
Description copied from class:Logger
Associates this Logger with a new Configuration. This method is not exposed through the public API.There are two ways this could be used to guarantee all threads are aware of changes to config.
- Synchronize this method. Accessors don't need to be synchronized as Java will treat all variables within a synchronized block as volatile.
- Declare the variable volatile. Option 2 is used here as the performance cost is very low and it does a better job at documenting how it is used.
- Overrides:
updateConfiguration
in classLogger
- Parameters:
newConfig
- The new Configuration.
-
getNanoClock
NanoClock getNanoClock() -
getCachedTranslator
-
logMessage
Description copied from interface:ExtendedLogger
Logs a message at the specified level. It is the responsibility of the caller to ensure the specified level is enabled.- Specified by:
logMessage
in interfaceExtendedLogger
- Overrides:
logMessage
in classLogger
- Parameters:
fqcn
- The fully qualified class name of the logger entry point, used to determine the caller class and method when location information needs to be logged.level
- The logging Level to check.marker
- A Marker or null.message
- The Message.thrown
- the exception to log, including its stack trace.
-
log
-
getTranslatorType
-
isReused
-
logWithThreadLocalTranslator
private void logWithThreadLocalTranslator(String fqcn, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.This re-uses a
RingBufferLogEventTranslator
instance cached in aThreadLocal
to avoid creating unnecessary objects with each event.- Parameters:
fqcn
- fully qualified name of the callerlevel
- level at which the caller wants to log the messagemarker
- message markermessage
- the log messagethrown
- aThrowable
ornull
-
logWithThreadLocalTranslator
private void logWithThreadLocalTranslator(String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.This re-uses a
RingBufferLogEventTranslator
instance cached in aThreadLocal
to avoid creating unnecessary objects with each event.- Parameters:
fqcn
- fully qualified name of the callerlocation
- the Location of the caller.level
- level at which the caller wants to log the messagemarker
- message markermessage
- the log messagethrown
- aThrowable
ornull
-
publish
-
handleRingBufferFull
-
initTranslator
private void initTranslator(RingBufferLogEventTranslator translator, String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) -
initTranslator
-
initTranslatorThreadValues
-
calcLocationIfRequested
Returns the caller location if requested,null
otherwise.- Parameters:
fqcn
- fully qualified caller name.- Returns:
- the caller location if requested,
null
otherwise.
-
logWithVarargTranslator
private void logWithVarargTranslator(String fqcn, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.This creates a new varargs Object array for each invocation, but does not store any non-JDK classes in a
ThreadLocal
to avoid memory leaks in web applications (see LOG4J2-1172).- Parameters:
fqcn
- fully qualified name of the callerlevel
- level at which the caller wants to log the messagemarker
- message markermessage
- the log messagethrown
- aThrowable
ornull
-
logWithVarargTranslator
private void logWithVarargTranslator(String fqcn, StackTraceElement location, Level level, Marker marker, Message message, Throwable thrown) Enqueues the specified log event data for logging in a background thread.This creates a new varargs Object array for each invocation, but does not store any non-JDK classes in a
ThreadLocal
to avoid memory leaks in web applications (see LOG4J2-1172).- Parameters:
fqcn
- fully qualified name of the callerlocation
- location of the caller.level
- level at which the caller wants to log the messagemarker
- message markermessage
- the log messagethrown
- aThrowable
ornull
-
translateTo
- Specified by:
translateTo
in interfacecom.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent>
-
logMessageInCurrentThread
void logMessageInCurrentThread(String fqcn, Level level, Marker marker, Message message, Throwable thrown) LOG4J2-471: prevent deadlock when RingBuffer is full and object being logged calls Logger.log() from its toString() method- Parameters:
fqcn
- fully qualified caller namelevel
- log levelmarker
- optional markermessage
- log messagethrown
- optional exception
-
handleRingBufferFull
-
actualAsyncLog
This method is called by the EventHandler that processes the RingBufferLogEvent in a separate thread. Merges the contents of the configuration map into the contextData, after replacing any variables in the property values with the StrSubstitutor-supplied actual values.- Parameters:
event
- the event to log
-
onPropertiesPresent
-
getContextData
-