When not to use AsyncAppender in logback by default

前端 未结 1 571
梦谈多话
梦谈多话 2021-02-07 16:09

Logback supports using an async appender with the class ch.qos.Logback.classic.AsyncAppender and according to the documentation, this will reduce the logging overh

相关标签:
1条回答
  • 2021-02-07 16:09

    The AsyncAppender acts as a dispatcher to another appender. It buffers log events and dispatches them to, say, a FileAppender or a ConsoleAppender etc.

    • Why use the AsyncAppender?

    • The AsyncAppender buffers log events, allowing your application code to move on rather than wait for the logging subsystem to complete a write. This can improve your application's responsiveness in cases where the underlying appender is slow to respond e.g. a database or a file system that may be prone to contention.

    • Why not make it the default behavior?

    • The AsyncAppender cannot write to a file or console or a database or a socket etc. Instead, it just delegates log events to an appender which can do that. Without the underlying appender, the AsyncAppender is, effectively, a no-op.

    • The buffer of log events sits on your application's heap; this is a potential resource leak. If the buffer builds more quickly than it can be drained then the buffer will consume resources that your application might want to use.

    • The AsyncAppender's need for configuration to balance the competing demands of no-loss and resource leakage and to handle on-shutdown draining of its buffer means that it is more complicated to manage and to reason about than simply using synchronous writes. So, on the basis of preferring simplicity over complexity, it makes sense for Logback's default write strategy to be synchronous.

    The AsyncAppender exposes configuration levers that you can use to address the potential resource leakage. For example:

    • You can increase the buffer capacity
    • You can instruct Logback to drop events once the buffer reaches maximum capacity
    • You can control what types of events are discarded; drop TRACE events before ERROR events etc

    The AsyncAppender also exposes configuration levers which you can use to limit (though not eliminate) the loss of events during application shutdown.

    However, it remains true that the simplest safest way of ensuring that log events are successfully written is to write them synchronously. The AsyncAppender should only be considered when you have a proven issue where writing to an appender materially affects your application responsiveness/throughput.

    0 讨论(0)
提交回复
热议问题