Reputation: 45
It turns out that log4j2 may be unsafe sometimes. 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 ?
Upvotes: 1
Views: 3034
Reputation: 14671
This can be set with the ignoreExceptions flag on the appender:
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
Upvotes: 1