Create stack levels in Django HTML email reports

Django has an amazing debug page that appears whenever there is an exception in the code. This page shows all the levels on the stack compactly, and you can expand any level that interests you. It is displayed only in debug mode.

Django also has a great feature for sending reports by email when actual users encounter errors on the production server. These reports contain a stack with much less information than a stylized debug page.

There is an amazing optional parameter 'include_html': True , which forces the email to include all the information on the debug page, which is really useful. The problem with this option is that the HTML seems to be unoccupied, so all of these stack levels expand to show all the data that they contain.

The result is such a long letter that GMail usually cannot display it without sending you to the highlighted view. But the real problem is that it is too big to navigate and search for the desired stack level.

I want: I want Django to send this detailed stack stack, but I want the stack levels to be reset as on the debug page. How can i do this?

(And no, I don't want to use Sentry.)

+6
source share
3 answers

We use middleware to handle any exceptions that occur during production. The main idea is to save the debug html to a location in the file system that can be sent via a password-protected view, and then send a link to the generated view to those who need it.

Here is the basic implementation:

 from django.conf import settings from django.core.mail import send_mail from django.views import debug import traceback import hashlib import sys class ExceptionMiddleware(object): def process_exception(self, request, exception): if isinstance(exception, Http404): return traceback = traceback.format_exc() traceback_hash = hashlib.md5(traceback).hexdigest() traceback_name = '%s.html' % traceback_hash traceback_path = os.path.join(settings.EXCEPTIONS_DIR, traceback_name) reporter = debug.ExceptionReporter(request, *sys.exc_info()) with open(traceback_path, 'w') as f: f.write(reporter.get_traceback_html().encode("utf-8")) send_mail( 'Error at %s' % request.path, request.build_absolute_uri(reverse('exception', args=(traceback_name, ))), FROM_EMAIL, TO_EMAIL, ) 

And submission

 from django.conf import settings from django.http import HttpResponse def exception(request, traceback_name): traceback_path = os.path.join(settings.EXCEPTIONS_DIR, traceback_name) with open(traceback_path, 'r') as f: response = HttpResponse(f.read()) return response 

Full middleware is tailored to our needs, but the basics exist. You should probably protect your password somehow. If you do not return Django's answer, then error handling will be deleted and will send you an email, but I will leave it to you.

+2
source

Tracking on the debug page is stackable because they run in javascript . I doubt that you can have what you want as HTML letters should not accept javascript .

+1
source

As JC LeitΓ£o noted, the django error debug page has javascript and css (most css doesn't work in email). But all of these css and js codes are inline. The debug page is a single html file that has no external resources.

At my company, we include html as an attachment in an email message. When we feel that the plain text trace is not clear enough, we load the html page and open it. The user interface is not as good as Sentry, but much better than the plain text version.

+1
source

Source: https://habr.com/ru/post/970093/


All Articles