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

标签 java logging exception log4j2

在我的应用程序中,我在异步模式下使用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.)

等待建议。

最佳答案

我建议首先尝试实现一个简单的类,该类实现了 ExceptionHandler 接口(interface),并确认在出现问题时它会被调用。一旦确认,您就可以继续实现您的自定义后备机制。

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

硬件很便宜,因此请考虑为您的后备机制添加单独的网卡或单独的硬盘。如果使用回退机制,请不要忘记发送通知,以便解决最初的问题。

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

关于java - 使用Log4j2异步日志记录时如何处理日志记录异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29382864/

相关文章:

java - junit-quickcheck - 生成保持深度有限的随机树

database - 寻找基于网络的日志收集/显示应用程序

elasticsearch - log4j2.properties的每日翻转不起作用

java - 如何使 jaxb-xjc Throwable 使用 "Exception"类型类?

java - Java中线程安全类的解释

java - Im4Java 是否支持在指定字体、字体大小和颜色的同时向图像添加文本?

docker - 如何从控制台或Docker容器读取日志

c# - 通过 wcf 服务调用时数据库连接不起作用

java - Android 抛出 IndexOutOfBound 异常

java - 使用 spring-integration 列出 ftp 中的文件