Class AsyncLogger

All Implemented Interfaces:
com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent>, Serializable, Logger, ExtendedLogger, LocationAwareLogger, Supplier<LoggerConfig>

public class AsyncLogger extends Logger implements com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent>
AsyncLogger is a logger designed for high throughput and low latency logging. It does not perform any I/O in the calling (application) thread, but instead hands off the work to another thread as soon as possible. The actual logging is performed in the background thread. It uses LMAX Disruptor for inter-thread communication.

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:
  • Constructor Details

    • AsyncLogger

      public AsyncLogger(LoggerContext context, String name, MessageFactory messageFactory, org.apache.logging.log4j.core.async.AsyncLoggerDisruptor loggerDisruptor)
      Constructs an AsyncLogger with the specified context, name and message factory.
      context - context of this logger
      name - name of this logger
      messageFactory - message factory of this logger
      loggerDisruptor - helper class that logging can be delegated to. This object owns the Disruptor.
  • Method Details

    • updateConfiguration

      protected void updateConfiguration(Configuration newConfig)
      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.

      1. Synchronize this method. Accessors don't need to be synchronized as Java will treat all variables within a synchronized block as volatile.
      2. 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.
      updateConfiguration in class Logger
      newConfig - The new Configuration.
    • logMessage

      public void logMessage(String fqcn, Level level, Marker marker, Message message, Throwable thrown)
      Specified by:
      logMessage in interface ExtendedLogger
      logMessage in class Logger
    • log

      public void log(Level level, Marker marker, String fqcn, StackTraceElement location, Message message, Throwable throwable)
      log in class Logger
    • translateTo

      public void translateTo(RingBufferLogEvent event, long sequence, Object... args)
      Specified by:
      translateTo in interface com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent>
    • actualAsyncLog

      public void actualAsyncLog(RingBufferLogEvent event)
      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.
      event - the event to log