Last active
January 1, 2018 06:48
-
-
Save guanidene/ab8d01a509956aaed296ba17e8ec41de to your computer and use it in GitHub Desktop.
Code to log assertions and any uncaught exceptions without extra effort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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