Adding function to sys.excepthook

前端 未结 5 679
不知归路
不知归路 2021-01-13 12:02

Say I have something like this, which sends unhanded exceptions to logging.critical():

import sys

def register_handler():
    orig_excepthook =         


        
5条回答
  •  轮回少年
    2021-01-13 12:44

    You should use sys.__excepthook__[1].
    It is an object that contains the original values of sys.excepthook[2] at the start of the program.
    ie;

    import sys
    import logging
    
    def register_handler():
        log = logging.getLogger(__name__)
        def error_catcher(*exc_info):
            log.critical("Unhandled exception", exc_info=exc_info)
            sys.__excepthook__(*exc_info)
    
        sys.excepthook = error_catcher
    

    Reference:
    1. https://docs.python.org/3/library/sys.html#sys.excepthook
    2. https://docs.python.org/3/library/sys.html#sys.excepthook

提交回复
热议问题