Python: log uncaught exceptions with sys.excepthook

You ever notice how when your script dies because of some uncaught error, you don’t get that error in your log files? This post walks through how to make sure that you log that uncaught exception.

This is a trivial script that will raise an uncaught exception (code available here):

$ cat rgl/
# vim: set expandtab ts=4 sw=4 filetype=python:

import logging

def f():
return g()

def g():
return h()

def h():
return i()

def i():

if __name__ == '__main__':


logging.debug('About to do f().')


Notice the helpful traceback:

$ python rgl/
Traceback (most recent call last):
File "rgl/", line 28, in
File "rgl/", line 9, in f
return g()
File "rgl/", line 13, in g
return h()
File "rgl/", line 17, in h
return i()
File "rgl/", line 21, in i
ZeroDivisionError: integer division or
modulo by zero

Unfortunately, that helpful traceback does not show up in the output logs!

$ cat /tmp/kaboom1.log
DEBUG:root:About to do f().

You could wrap your code with big try / except

This diaper pattern is a popular solution::


except Exception as ex:

Make sure you re-raise the exception, otherwise your program will end with a zero return code.

Sidenote: how to log an exception instance

If you do any of these, you probably won’t like what you get:


In both cases, you are just turning the exception to a string. You won’t see the traceback and you won’t see the exception type.

Instead of those, make sure you do one of these:


# this is exactly what logging.exception does inside
logging.error(ex, exc_info=1)

# sets a higher log level than error
logging.critical(ex, exc_info=1)

For the last two, without that exc_info=1 parameter, you won’t see the traceback in your logs. You’ll just see the message from the exception.

Or you can use sys.excepthook

Instead of nesting your code inside a try-except clause, you can customize the built-in sys.excepthook function.

The script has this extra code:

def log_uncaught_exceptions(ex_cls, ex, tb):

logging.critical('{0}: {1}'.format(ex_cls, ex))

sys.excepthook = log_uncaught_exceptions

And here’s the results:

$ python rgl/

$ cat /tmp/kaboom2.log
DEBUG:root:About to do f().
CRITICAL:root: File "rgl/", line 39, in
File "rgl/", line 9, in f
return g()
File "rgl/", line 13, in g
return h()
File "rgl/", line 17, in h
return i()
File "rgl/", line 21, in i

CRITICAL:root:: integer division or modulo by zero

Incidentally, sys.excepthook preserves the non-zero return code.
Also incidentally, you can use sys.excepthook for all sorts of fun stuff. This shows how to make it fire off pdb when stuff blows up.

Published by


My name is Matt Wilson and I live in Cleveland Heights, Ohio. I love random emails from strangers, so get in touch!

  • Better then that would be logging as “logger.critical('Whatever', exc_info=(exc_type, exc_value, tb))“

  • One thing to watch out for is that Apport, the crash reporting tool on Ubuntu, overrides sys.excepthook for you already. It does this by including an Apport hook in

    $ python -c 'import sys; print repr(sys.excepthook)'
    <function 0xb71ee87c="" apport_excepthook="" at="">
    $ cat /etc/python2.7/
    # install the apport exception handler if available
        import apport_python_hook
    except ImportError:

  • Federico

    Full example:
    def log_uncaught_exceptions(*exc_info): 
        logging.critical('Unhandled exception:', exc_info=exc_info) 

  • Thanks for the feedback!

  • ??????? ???????

    What is the profit of logging.critical(”.join.traceback.format…. in hook? why not just use logging.exception(ex) oneliner there?

  • yazinsai

    Awesome! Thanks for sharing.. great for catching leaking exceptions without making the codebase all icky..