Logging all uncaught exceptions

Steven D'Aprano steve at pearwood.info
Mon Jul 14 02:09:32 EDT 2014


I want to catch all uncaught exceptions in my application, log them, then 
handle as normal. Which, in practice, means a traceback. Is this the 
right way to do it?



import logging
import sys

logger = logging.getLogger('mylogger')

def my_handler(type, value, tb):
    msg = "Uncaught %s: %s" % (type, value)
    logger.exception(msg)
    sys.__excepthook__(type, value, tb)


# Install exception handler
sys.excepthook = my_handler

# Run your main script here:
if __name__ == '__main__':
    main()





More information about the Python-list mailing list