Skip to content

Instantly share code, notes, and snippets.

@guanidene
Last active January 1, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guanidene/ab8d01a509956aaed296ba17e8ec41de to your computer and use it in GitHub Desktop.
Save guanidene/ab8d01a509956aaed296ba17e8ec41de to your computer and use it in GitHub Desktop.
Code to log assertions and any uncaught exceptions without extra effort
# Put this code in the top most level module. Any assertions or uncaught exceptions firing within this module or any of the submodules would be caught by the logger.
def excepthook(*args):
logging.getLogger(<give_correct_logname_here>).error('Uncaught exception:', exc_info=args)
sys.excepthook = excepthook
# Test the above code...
assert 1==2, 'Something went wrong'
# Credits:
# http://code.activestate.com/recipes/577074-logging-asserts/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment