For this, you can use the sys.excepthook that allows us to attach a handler
for any unhandled exception:
First, you will create a logger that will take care of your exceptions.
Then create a handler handle_unhandled_exception that you will later attach to
the hook. Then call the __excepthook__ provided by the sys module. This
method is invoked every time the exception is uncaught.
Use the info from the hook to create a log message and assign this handler to
the sys.excepthook.
You can learn more info about sys.excepthook in
the offical documentation..
If you are new to logging in Python, please feel free read our Introduction to Python logging and learn more.
🔭 Want to centralize and monitor your python logs?
Go to Logtail and start your log management in 5 minutes.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.