How to Auto‑Email Django Errors: Configure Logging & Custom Decorators
Learn how to set up Django to automatically email error logs for both request and non‑request failures by configuring email settings, LOGGING handlers, and creating a reusable decorator, complete with code examples and screenshots of the monitoring results.
Monitor All Request Requests
Configure Django to send error emails for request‑related failures by setting the email parameters in the settings.py file. The emails are sent to the addresses defined in ADMINS.
SERVER_EMAIL = '[email protected]'
DEFAULT_FROM_EMAIL = '[email protected]'
ADMINS = (('receiver','[email protected]'),)
EMAIL_HOST = 'smtp.exmail.qq.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = '123456'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'1. Set Email Configuration
The above settings ensure that Django can connect to the SMTP server and send messages to the administrators.
2. Configure LOGGING
Define a mail_admin handler that uses django.utils.log.AdminEmailHandler and set its level to ERROR. This handler will automatically email 5XX server errors.
'handlers': {
'mail_admin': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': False,
}
}Attach the handler to the django.request logger so that request‑related errors are captured.
'loggers': {
'django.request': {
'handlers': ['default', 'mail_admin'],
'propagate': True,
'level': 'ERROR',
}
}Monitor Non‑Request Tasks
For background jobs such as scheduled tasks, create a custom decorator that catches exceptions, formats the traceback, and sends an email using a utility function.
def decorator_error_monitor(title):
def wrap(f):
def wrapped_f(*args, **kwargs):
try:
result = f(*args, **kwargs)
return result
except:
exc = traceback.format_exc()
utils.send_exception_email(email_list, title, exc)
raise Exception(exc)
return wrapped_f
return wrapApply the decorator to any function that needs monitoring:
@decorator_error_monitor("Settlement Error")
def do_settlement(users):
for user in users:
process_settlement_for_one_user(user)Conclusion
The presented monitoring methods are simple and practical, requiring no additional logging infrastructure. They enable immediate detection of system issues and provide detailed error logs to help quickly locate and resolve problems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
