Created
July 17, 2025 17:25
-
-
Save stringertheory/4b90fcd980d9b0e5c6cdc2f81b64e50b to your computer and use it in GitHub Desktop.
A function to use to make sure that tracebacks are logged.
This file contains hidden or 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
| def excepthook(*exc_info): | |
| """Log the traceback if program throws an unhandled exception.""" | |
| exc_type, exc_value, exc_traceback = exc_info | |
| exc_time = time.time() | |
| msg = f"{exc_type.__name__} @ {exc_time}" | |
| logging.critical(f"{msg}. Traceback ↓", exc_info=exc_info) | |
| logging.critical(f"{msg}. Traceback ↑") | |
| sys.excepthook = excepthook |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creates output in logs like this:
The timestamp is in the log message at the beginning and end of the traceback, so that it's easy to search for the beginning/end of the traceback when reading the logfile.