5

How do i set the number of threads for Async Logger and Async Appender in log4j2? Is it using one thread by default?

I tried running for a batch of 100,000 records, i dont see any difference in the last log statement time and end timestamp of application exit displayed using system.out.print. How can i prove that this is logging asynchronous?

<RollingFile name="APP.ALERT" fileName="C:\Users\sbasheer\Downloads\abc\Alert.log" 
 filePattern="C:\Users\sbasheer\Downloads\abc\Alert%d{MM-dd-yyyy}-%i.log" immediateFlush="true" append="true">
  <PatternLayout>
    <Pattern>%d %p %class{1.} [%t] %location %m %ex%n</Pattern>
  </PatternLayout>
  <Policies>
    <TimeBasedTriggeringPolicy />
    <SizeBasedTriggeringPolicy size="25 MB"/>
  </Policies>
  <DefaultRolloverStrategy max="50"/>
</RollingFile>


 <AsyncLogger name="com.abc.asyncsample"  level="trace" includeLocation="true" additivity="true"> 
  <AppenderRef ref="APP.ALERT" level="error" />
</AsyncLogger>

1 Answer 1

6

By design both Async Loggers and AsyncAppender have only one thread that does the I/O. They are both asynchronous, so your application's call to Logger.debug(...) returns immediately.

Async Loggers are different from AsyncAppender in that Async Loggers use a non-blocking data structure (the LMAX Disruptor) to enqueue log events. The advantage of this kicks in when your application has many threads that are logging at the same time. The non-blocking queue means that your application threads don't need to contend for a lock, giving much better throughput and lower/more predictable latency.

The background thread then takes the log events off the queue and writes them to the I/O device in batches, which is very efficient.

Because there is only one background thread, this thread also does not need to contend with other threads for the lock on the I/O device, again giving better performance.

The timestamp in the log is the timestamp when the event was created by the application thread, which is not the same as the time when the event is actually written to disk.

How to prove that logging is asynchronous? You could run your application in a debugger; you should see one thread for your application and one thread for the AsyncLoggerConfig. If you pause the AsyncLogger thread in your IDE, no more events are written to disk, but your application thread will still continue to call Logger.log, which proves that logging is asynchronous.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Remko. So if i have 30 threads in my application, there will be one single thread for AsyncLoggerConfig reading from the non blocking data structure? Is there an option to increase the number of background threads for logging or one single thread scales enough?
Yes, there is only one thread that reads from the queue, and your app can have as many threads as you want. No, there is no option to increase the number of background threads (and a single thread likely scales better since it does not need to contend with other threads for the lock on the IO device).
@RemkoPopma why did you used LMAX Disruptor you could have used any other non blocking data structure here.
@UmarTahir perhaps that deserves its own StackOverflow question; can you create a question for this instead of a comment?
Yeah sure, give me a moment I will create and share it with you thorugh comment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.