禁用 log4j 2 异常(apache 异常)

Disable log4j 2 exceptions (apache's exeptions)

原来log4j2有时候可能不安全。记录消息可能会引发异常,从而破坏功能。我认为这不是理想的日志行为 - 日志记录应该在没有崩溃功能的情况下失败。这是一个基本示例:

InstrumentedAppender = new InstrumentedAppender (..);
appender.start();
...
appender.stop();
final Logger logger = LogManager.getLogger("LOG4J_TEST");
try {
  logger.error("TEST_ERROR");
} catch (Exception e){
  System.out.println("-------------- Exception is thrown");
  e.printStackTrace()
}

在我的输出中我得到了这个:

org.apache.logging.log4j.core.appender.AppenderLoggingException: Attempted to append to non-started appender org.apache.logging.log4j.core.Appender
        at org.apache.logging.log4j.core.config.AppenderControl.handleError(AppenderControl.java:142)
        at org.apache.logging.log4j.core.config.AppenderControl.ensureAppenderStarted(AppenderControl.java:135)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:127)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
        at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
...........

有什么方法可以在我的应用程序中避免这种日志行为?

这可以通过 appender 上的 ignoreExceptions 标志来设置:

The default is true, causing exceptions encountered while appending events to be internally logged and then ignored. When set to false exceptions will be propagated to the caller, instead. You must set this to false when wrapping this Appender in a FailoverAppender.

https://logging.apache.org/log4j/2.x/manual/appenders.html