When using Logback's AsyncAppender, messages get queued before they
are written to their final destination (depending on which Appender
you wrapped with the AsyncAppender). So far so good.
Is it possible those queued messages get lost when my program or logback itself crash?
Yes, I think queued messages will be lost when JVM crashed and even normal program shutdown.
I was doing an investigation on how much time is improved when using
AsyncAppender.Sample logback.xml:
I have a normal FILE_LOG and ASYNC_FILE_LOG.
Sample MultiThreadsExecutor tester program, it should generate 1010 lines of log messages:
Test Case (1) Normal execution.
ASYNC_FILE_LOG indeed is much faster. However...
File.log recorded 1010 line of messages correctly:
Async_File.log records up to 800+ messages only!
Test Case (2) , Tester sleeps 1 second before END in case AsyncAppender needs time to clear queue...
ASYNC_FILE_LOG is still faster. This time, ASYNC_FILE_LOG recorded 1010 lines correctly.
Conclusion
The above test proves that, when Java program shutdown,
AsyncAppenderdoes not have enough time to clear log queue. Not to mention JVM crashes, Java program exit is immediate.