Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:16:24+00:00 2026-06-12T12:16:24+00:00

I’ve been wanting to make my exception e-mails a bit easier to read /

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:16:26+00:00Added an answer on June 12, 2026 at 12:16 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.