问题
For asynchronous logging, what happens to the logs that are not yet written to the destination appender and the application/system goes down? Do we loose these logs (since they are in memory) or is there a way to recover them?
I don't want to loose any ERROR level logging. So my next question is: Is there a way to configure sync and async logging for a single appender at log level? For e.g. I want my file appender to perform ERROR level logging synchronously while INFO/DEBUG asynchronously?
Thanks in advance!
回答1:
If the process dies gracefully, all events in the queue will be processed. See this code for how AsyncLogger shutdown works in detail.
If the process is not allowed to do cleanup (kill -9
for example) then you may lose events. Such is life.
If you want ERROR level log events processed synchronously but other levels asynchronously you may not be able to use AsyncLoggers but you can use AsyncAppender. You can configure the Logger(s) to route to both a FileAppender and an AsyncAppender. The AsyncAppender routes to the FileAppender. Then use Filters to make the AsyncAppender only accept INFO and below, while the FileAppender accepts ERROR and above.
来源:https://stackoverflow.com/questions/44035629/how-does-log42-async-logger-behaves-on-sudden-shutdown-of-application