Say I have something like this, which sends unhanded exceptions to logging.critical()
:
import sys
def register_handler():
orig_excepthook =
If you make orig_excepthook
an argument with a default value, the default value is fixed once at definition-time. So repeated calls to register_handler
will not change orig_excepthook
.
import sys
def register_handler(orig_excepthook=sys.excepthook):
def error_catcher(*exc_info):
import logging
log = logging.getLogger(__name__)
log.critical("Unhandled exception", exc_info=exc_info)
orig_excepthook(*exc_info)
sys.excepthook = error_catcher
import logging
logging.basicConfig()
register_handler()
register_handler()
register_handler()
undefined()
produces only one call to log.critical
.