Assume our logging configuration is defined with some logging handlers. There is a way to customize such a handler, so I can define any operation on the emit function of any handler. Assume furthermore, that one of the handler raises an exception. The question is that, no matter what, the remaining ones' emit function is still triggered, or some will silently fail.
A possible use case would be to use the predefined SMTPHandler, but with the internet being shut down. If a RotatingFileHandler is defined in the scope, can it fail in that case, or the emitting of the handlers happen independently?
I see three possible cases for the example:
The Python standard-library handlers have been built with robustness in mind. If an exception is raised while a handler is asked to emit a record, all standard library implementations catch the exception and instead call Handler.handleError()
.
By default, and if sys.stderr
is not set to None
, calling Handler.handleError()
re-raises the exception. In production systems you want to set logging.raiseExceptions = False
, at which point the exceptions are silently ignored, and logging continues as if nothing happened.
From the documentation:
This method should be called from handlers when an exception is encountered during an
emit()
call. If the module-level attributeraiseExceptions
isFalse
, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The specified record is the one which was being processed when the exception occurred. (The default value ofraiseExceptions
isTrue
, as that is more useful during development).
So if logging.raiseExceptions
is left to the default True
, then it depends on the handler registration order which handler is called first. Any handlers called before an exception is raised will have succeeded.
If logging.raiseExceptions
has been set to False
, all handlers will be called, even if one of them fails with an exception.