使用 Log4j2 异步日志记录时如何处理日志记录中的异常?

How to handle exceptions in logging when use Log4j2 Async logging?

在我的应用程序中,我在异步模式下使用 Log4j2(使用 Disruptor 的异步记录器),有人可以告诉我如何处理记录过程中发生的异常。日志记录对我的应用程序非常重要,我不想错过任何一条日志语句。如果日志记录出现问题,需要立即发出警报。

根据 Apache Log4j page,建议使用 ExceptionHandler,但我找不到有关如何使用它的帮助主题。

Error handling. If a problem happens during the logging process and an exception is thrown, it is less easy for an asynchronous logger or appender to signal this problem to the application. This can partly be alleviated by configuring an ExceptionHandler, but this may still not cover all cases. For this reason, if logging is part of your business logic, for example if you are using Log4j as an audit logging framework, we would recommend to synchronously log those audit messages. (Note that you can still combine them and use asynchronous logging for debug/trace logging in addition to synchronous logging for the audit trail.)

等待建议。

我建议首先尝试实现一个简单的 class,它实现了 ExceptionHandler 接口并确认它在出现问题时被调用。确认后,您可以继续实施自定义回退机制。

其次,可能很难开发出可靠的后备方案:如果 log4j 失败的原因是磁盘已满或损坏,则您的后备方案可能无法写入磁盘。同样,您可能无法建立网络连接......我建议您的回退包含多个选项,以增加其中一个选项成功的可能性。

硬件很便宜,所以考虑为您的回退机制添加一个单独的网卡或一个单独的硬盘。如果使用回退机制,请不要忘记发送通知,以便您可以解决原始问题。

根据这对任务的关键程度,您可能想要调查提供高可用性的供应商产品 - 这通常不仅仅涵盖日志记录,不确定您的需求是什么。