More informative tracebacks [code]

Bjorn Pettersen BPettersen at NAREX.com
Fri Aug 30 18:35:53 CEST 2002


I just created the following code snippet to get tracebacks emailed back
to me when they happen in environments I don't have control over. I
thought it might be useful for someone...

Comments and improvements are welcome :-)

-- bjorn

import sys, cgitb
from cStringIO import StringIO

class EMailExcept(cgitb.Hook):
    def __init__(self, emailaddress):
        self.errout = StringIO()
        self.email = emailaddress
        cgitb.Hook.__init__(self, 1, 'exlog', 5, self.errout)
        
    def handle(self, info=None):
        cgitb.Hook.handle(self, info)
        errtxt = self.errout.getvalue()
        import smtplib
        from email.Generator import Generator
        from email.MIMEText import MIMEText
        
        msg = MIMEText(errtxt, 'html')
        msg['Subject'] = 'Traceback from: ' + sys.argv[0]
        msg['From'] = 'Python'
        msg['To'] = self.email
        
        out = StringIO()
        g = Generator(out)
        g(msg)
        
        s = smtplib.SMTP()
        s.connect('exchange')
        s.sendmail('Python', [self.email], out.getvalue())
        s.close()
        print errtxt # to visually notify operators
        sys.exit(-1) # to signify failure if running in a batch system
    
sys.excepthook = EMailExcept('abuse at yahoo.com')




More information about the Python-list mailing list