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 9004067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:49:06+00:00 2026-06-16T00:49:06+00:00

I’ve got error emails setup via Django’s logging mechanism in 1.3. It sends me

  • 0

I’ve got error emails setup via Django’s logging mechanism in 1.3. It sends me a nice email when an error happens. However, when I log a simple error message it’s being formatted oddly and I’m not sure why.

For example, there’s a condition in my app where if something doesn’t exist in the DB I want to know about, but I have a suitable default value that will work fine. Thus, I want an email sent to me with some info; it’s not necessarily happening on an Exception.

If I do something like this:

logger.error(“fee did not exist in the database for action %s”, “actionX”)

The information in the logfile is fine, but the email is really lacking some information. Here’s the subject line:

[Django] ERROR: Test this jazz %s

And then the body:

None

Request repr() unavailable

My question is, how do I get A) the value to show up in the subject and B) get some actual, relevant information in the body….like line number or something like that.

  • 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-16T00:49:08+00:00Added an answer on June 16, 2026 at 12:49 am

    You need to acknowledge two things:

    1. You want to send an email using Python’s builtin logging system and
    2. You are not logging a regular exception so the builtin mechanism for sending emails won’t work since it’s depending on an exception type-like object to be passed and stuff to be stored in a traceback.

    Anyways, not impossible!

    LOGGING = {
       ...
       'handlers': {
            ...
            'my_special_mail_handler': {
                'level': 'ERROR',
                'filters': [],
                'class': 'myapp.loggers.MyEmailHandler',
                'include_html': False,
            },
        },
        'loggers': {
            ...
            'my_special_logger': {
                'handlers': ['console', 'my_special_mail_handler'],
                'level': 'DEBUG',
                'propagate': True,
            },
        }
    }
    
    MY_RECIPIENTS = (("Name of person", "email@example.com"),)
    

    …that stuff is merged into your settings.

    Then, there’s your special logging class, MyEmailHandler:

    from django.utils.log import AdminEmailHandler
    from django.core.mail.message import EmailMultiAlternatives
    from django.conf import settings
    
    class MyEmailHandler(AdminEmailHandler):
    
        def emit(self, record):
            if not getattr(settings, "MY_RECIPIENTS", None):
                return
            subject = self.format_subject(record.getMessage())
            message = getattr(record, "email_body", record.getMessage())
            mail = EmailMultiAlternatives(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
                        message, settings.SERVER_EMAIL, [a[1] for a in settings.MY_RECIPIENTS],)
            mail.send(fail_silently=False)
    

    Now you’re able to create a special logging entry that’s both emailed and output to the terminal this way:

    import logging
    logger = logging.getLogger("my_special_logger")
    error = logger.makeRecord(
        logger.name, logging.ERROR, 0, 0,
        u"Subject: Some error occured",
        None, None, "", None,
    )
    error.email_body = msg
    logger.handle(error)
    

    and to make stuff easy, use a utility function:

    import logging
    def my_log(msg, body, level=logging.ERROR):
        logger = logging.getLogger("my_special_logger")
        error = logger.makeRecord(
            logger.name, level, 0, 0,
            u"Subject: Some error occured",
            None, None, "", None,
        )
        error.email_body = msg
        logger.handle(error)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.