Search code examples
pythonemaillogginghtml-emailcolor-coding

Color coding traceback in the SMTPHandler and sending HTML


I've been wanting to make my exception e-mails a bit easier to read / navigate and I think that if I could color code the output, noticeably the traceback and some JSON prints, it would make my life much easier.

So this question is twofold:

  1. How can I setup my formatter for my SMTPHandler so that it spits out HTML, and have the SMTPHandler send the e-mails as HTML and not just plain text?

  2. Do you know of any libraries that would make it easy to take tracebacks and/or JSON and color code the output into HTML?


Solution

  • This solutions extends standard logging.SMTPHandler and uses pygments library to create colorful html version of traceback. It's not very elegant, because it has to use formatter private attribute: _fmt to construct additional log information, but it works (you can customize styles using pygments or directly in html variable):

    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.utils import formatdate
    import logging
    from logging.handlers import SMTPHandler
    from pygments import highlight
    from pygments.formatters import HtmlFormatter
    from pygments.lexers import PythonTracebackLexer
    import smtplib
    
    
    class ColorfulSMTPHandler(SMTPHandler):
    
        def emit(self, record):
            try:
                port = self.mailport
                if not port:
                    port = smtplib.SMTP_PORT
                smtp = smtplib.SMTP(self.mailhost, port)
                msg = MIMEMultipart('alternative')
                msg['Subject'] = self.getSubject(record)
                msg['From'] = self.fromaddr
                msg['To'] = ",".join(self.toaddrs)
                msg['Date'] = formatdate()
    
                text = self.format(record)
                msg.attach(MIMEText(text, 'plain'))
                if record.exc_text:
                    html_formatter = HtmlFormatter(noclasses=True)
                    tb = highlight(record.exc_text, PythonTracebackLexer(), html_formatter)
    
                    info = (self.formatter or logging._defaultFormatter)._fmt % record.__dict__
                    info = '<p style="white-space: pre-wrap; word-wrap: break-word;">%s</p>' % info
    
                    html = ('<html><head></head><body>%s%s</body></html>')% (info, tb)
                    msg.attach(MIMEText(html, 'html'))
                if self.username:
                    if self.secure is not None:
                        smtp.ehlo()
                        smtp.starttls(*self.secure)
                        smtp.ehlo()
                    smtp.login(self.username, self.password)
                smtp.sendmail(self.fromaddr, self.toaddrs, msg.as_string())
                smtp.quit()
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                self.handleError(record)
    

    Edit: You can also use my logging handler from my fork of great-justice: https://github.com/paluh/great-justice-with-logging/blob/master/great_justice/logging.py#L85

    It generates really nice, informative traceback - the same format is used in emails and in terminal handler:

    enter image description here