I wrote a custom ErrorHandler that should shutdown the application if the logger is not able to log to its logfile.
import org.apache.logging.log4j.core.ErrorHandler;
public class LogFileErrorHandler extends ErrorHandler {
private static final Logger LOGGER = StatusLogger.getLogger();
@Override
public void error(String msg) {
LOGGER.error(msg);
System.exit(ExitCodes.IO_LOGGER_COULD_NOT_WRITE);
}
... more error implementations
}
Apparently there is no easy way to add it using the configuration programmatically.
Therefor I wrote this code as a workaround to add the ErrorHandler to my Appenders. It works and I can see that the ErrorHandler is added to my Appenders.
public void addErrorHandler() {
final LoggerContext context = (LoggerContext) LogManager.getContext();
for(Appender a : context.getConfiguration().getAppenders().values()) {
a.stop();
a.setHandler(new LogFileErrorHandler());
a.start();
}
}
I had to stop the appender before adding the ErrorHandler because otherwise I got the exception:
The handler cannot be changed once the appender is started
My question
I did a test where I locked the logfile in order to produce an IOException (Access denied). For some reason the Exception was handled by the DefaultErrorHandler instead of the custom one that I added.
Why is the exception not handled by my custom ErrorHandler?