Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between AsyncLogger and AsyncAppender in Log4j2

I have understanding that AsyncAppender do the appending job in a separate thread. They use ArrayBlockingQueue for this purpose.

AND

AsyncLogger uses LMAX disruptor library to move logging event from one application thread to the other thread and it is faster as compared to AsyncAppender.

My question is why do we have AsyncAppender in log4j2 if the job it does is achieved by AsyncLogger more efficiently.

What if we use AsyncAppender along with AsyncLogger ? Are there any more differences between AsyncLogger and AsyncAppender?

like image 350
saurabh goyal Avatar asked Jun 12 '14 06:06

saurabh goyal


People also ask

What is synchronous and asynchronous logging?

In synchronous mode, the logger writes entries directly to the destination. In asynchronous mode, the logger writes entries to a queue, then later writes the entries from the queue to the destination.

Is log4j synchronous or asynchronous?

Asynchronous Loggers are a new addition in Log4j 2. Their aim is to return from the call to Logger. log to the application as soon as possible. You can choose between making all Loggers asynchronous or using a mixture of synchronous and asynchronous Loggers.

What is AsyncLoggerContextSelector?

public class AsyncLoggerContextSelector extends ClassLoaderContextSelector. ContextSelector that manages AsyncLoggerContext instances. As of version 2.5, this class extends ClassLoaderContextSelector for better web app support.

How do I test async logging?

Another way to verify is by setting <Configuration status="trace"> at the top of your configuration file. This will output internal log4j log messages on log4j is configured. You should see something like "Starting AsyncLogger disruptor...". If you see this all loggers are async.


2 Answers

True, they achieve pretty much the same purpose, so I can understand your question: "why have both options"?

For background, the AsyncAppender has been in Log4j2 from the beginning, where Async Loggers were added in March last year (2014). That's how the current situation came to be.

The log4j team is not seriously considering removing the AsyncAppender at the moment. One thing to keep in mind is that Async Loggers have an external dependency (the LMAX disruptor jar) where the AsyncAppender works with just the log4j2-api and log4j2-core jars.

To answer your last question, it is possible to combine AsyncAppender with Async Loggers, but you will not gain anything. This has not been tested. I haven't checked but it is possible that there is a problem with location information getting lost when handing over the log event from the Async Logger thread to the AsyncAppender thread. I would not recommend doing this.

UPDATE (2014/6/23): I did some testing and there were a few issues with combining AsyncAppender with AsyncLoggers. These are fixed in RC2. I still don't recommend doing this, as it just adds another intermediate step that uses CPU/memory without contributing anything.

UPDATE (2016/7/20): Another difference: since version 2.6, Log4j 2 can be garbage-free with Async Loggers, but not with AsyncAppender.


In answer to your second question in the comments below: AsyncAppender has its own queue and thread, where AsyncLoggers use the LMAX Disruptor ringbuffer for a queue and uses an Executor thread.

like image 183
Remko Popma Avatar answered Sep 30 '22 15:09

Remko Popma


Not taking anything away from the accepted answer, but take a look at https://logging.apache.org/log4j/2.x/manual/async.html. The page includes performances measurements taken by the log4j2 team, where async loggers outperform async appenders by quite some margin.

like image 22
ftr Avatar answered Sep 30 '22 16:09

ftr