Configure Django to Email Error Logs for Requests and Tasks
This guide shows how to set up Django's email backend and logging configuration to automatically send detailed error reports for both HTTP requests and custom tasks, using simple settings changes and a reusable decorator for comprehensive system monitoring.
Without further ado, here is the monitoring effect: an email containing the request URL and full exception details is sent whenever an error occurs.
1. Monitor All Request Requests
Configure the email settings in settings.py so that error reports 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'2. Configure LOGGING
2.1 Set up the mail_admin handler
Define a handler that uses django.utils.log.AdminEmailHandler to send emails for error‑level logs:
'handlers': {
'mail_admin': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': False,
}
}2.2 Configure the django.request logger
Attach the mail_admin handler to the django.request logger so that any 5xx response triggers an email:
'loggers': {
'django.request': {
'handlers': ['default', 'mail_admin'],
'propagate': True,
'level': 'ERROR',
}
}3. Monitor Non‑Request Tasks
For background jobs such as scheduled tasks, create a decorator that catches exceptions and sends an email using a custom utils.send_exception_email 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 you want to monitor:
@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 effective, requiring no additional log‑monitoring system. They enable immediate detection of 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.
