Hello,
I have set up email error reporting in our Django application but we are finding the error email to be too verbose for our liking.
I have created a custom ExceptionReporter by extending the class, and have mostly achieved what we want, but I am now receiving two emails and would like guidance on a possibly more elegant solution.
I’ll paste the code below, as well as the two email contents.
class LessVerboseExceptionReporter(ExceptionReporter):
def get_traceback_data(self):
data = super().get_traceback_data()
reduced_report = {
"exception_value": data["exception_value"],
"is_email": data["is_email"],
"lastframe": data["lastframe"],
"frames": data["frames"],
"unicode_hint": data["unicode_hint"],
}
return reduced_report
import logging.config
LOGGING_CONFIG = None
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
'formatter': 'verbose'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': False,
'reporter_class': 'report.email.LessVerboseExceptionReporter'
},
},
'loggers': {
'': {
'handlers': ['console', 'mail_admins'],
'level': 'INFO',
'propagate': True,
},
},
}
logging.config.dictConfig(LOGGING)
Email 1 (Desirable):
Internal Server Error: /dept_awarding/update_batch_status/
Report
'str' object has no attribute 'status'
Django Version:
Python Executable:
Python Version:
Python Path:
Server time:
Installed Applications:
''
Installed Middleware:
''
Traceback (most recent call last):
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
^^^^^^^^^
File "C:\wamp64\www\apiano\win_env\Lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\wamp64\www\apiano\apiano_project\dept_awarding\views.py", line 133, in post
row_status.status.status
^^^^^^^^^^^^^^^^^
Request data not supplied
Settings:
Using settings module
Email 2 (Undesirable):
"POST /foo/update_bar_status/ HTTP/1.1" 500 145
Report
"POST /foo/update_bar_status/ HTTP/1.1" 500 145
Django Version:
Python Executable:
Python Version:
Python Path:
Server time:
Installed Applications:
''
Installed Middleware:
''
Traceback (most recent call last):
None