I'm initialize 2 servers in one twisted script. So I want to get all unhadled exception inside of it on my mail. Is it posible?
Thank you!
You can use sys.excepthook
. sys.excepthook
catches all exceptions that are uncaught in your code. How you handle them from there is your choice.
For sending emails, you may be interested in the Python email module. Here are some examples.
You can reconstruct a traceback from an exception using the Python traceback module; specifically format_exception()
.
import sys
import traceback
def excepthook(type_, value, tb):
# do whatever with the exception information
# you want to do here
exception = traceback.format_exception(type_, value, tb)
print ''.join(exception)
sys.excepthook = excepthook
Note: I don't know anything about twisted, so there may be a twisted way (pun not intended) to do this, however, this is how I would do it if there was no other official way of course.