简体   繁体   English

禁用 log4j 2 异常(apache 的异常)

[英]Disable log4j 2 exceptions (apache's exeptions)

It turns out that log4j2 may be unsafe sometimes.事实证明,log4j2 有时可能不安全。 Logging a message could throw exception which could break functionality.记录消息可能会引发异常,这可能会破坏功能。 I think that this is not desired log behaviour - logging should fail without crashing functionality.我认为这不是所需的日志行为 - 日志记录应该失败而不会导致功能崩溃。 Here's an basic example :这是一个基本示例:

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()
}

and in my output i get this:在我的输出中,我得到了这个:

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)
...........

Is there any way i can avoid this log behaviour in my application ?有什么办法可以在我的应用程序中避免这种日志行为?

This can be set with the ignoreExceptions flag on the appender:这可以使用 appender 上的 ignoreExceptions 标志设置:

The default is true, causing exceptions encountered while appending events to be internally logged and then ignored.默认值为 true,导致在附加事件时遇到异常以进行内部记录然后被忽略。 When set to false exceptions will be propagated to the caller, instead.当设置为 false 时,异常将传播给调用者。 You must set this to false when wrapping this Appender in a FailoverAppender.将此 Appender 包装在 FailoverAppender 中时,您必须将此设置为 false。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM