Difference between AsyncLogger and AsyncAppender in Log4j2

saurabh goyal picture saurabh goyal · Jun 12, 2014 · Viewed 8.1k times · Source

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?

Answer

Remko Popma picture Remko Popma · Jun 12, 2014

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.