Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><strong>Subclass AdminEmailHandler</strong> (defined in <a href="https://code.djangoproject.com/browser/django/trunk/django/utils/log.py" rel="noreferrer">django.utils.log</a>).</li> <li>Configure <a href="https://docs.djangoproject.com/en/dev/topics/logging/" rel="noreferrer">logging</a> accordingly.</li> </ol> <p>Here is how <code>AdminEmailHandler</code> works:</p> <pre><code>class AdminEmailHandler(logging.Handler): """An exception log handler that emails log entries to site admins. If the request is passed as the first argument to the log record, request data will be provided in the email report. """ def __init__(self, include_html=False): logging.Handler.__init__(self) self.include_html = include_html def emit(self, record): try: request = record.request subject = '%s (%s IP): %s' % ( record.levelname, (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), record.msg ) filter = get_exception_reporter_filter(request) request_repr = filter.get_request_repr(request) except: subject = '%s: %s' % ( record.levelname, record.getMessage() ) request = None request_repr = "Request repr() unavailable." if record.exc_info: exc_info = record.exc_info stack_trace = '\n'.join(traceback.format_exception(*record.exc_info)) else: exc_info = (None, record.getMessage(), None) stack_trace = 'No stack trace available' message = "%s\n\n%s" % (stack_trace, request_repr) reporter = ExceptionReporter(request, is_email=True, *exc_info) html_message = self.include_html and reporter.get_traceback_html() or None mail.mail_admins(subject, message, fail_silently=True, html_message=html_message) </code></pre> <hr /> <p><strong>For reference only: my previous answer.</strong></p> <ol> <li>Create a <strong>custom middleware</strong>: take inspiration from CommonMiddleware in <a href="https://code.djangoproject.com/browser/django/trunk/django/middleware/common.py" rel="noreferrer">https://code.djangoproject.com/browser/django/trunk/django/middleware/common.py</a> (have a look at <code>process_response</code>)</li> <li>Create a function <strong>send_mail_with_exception_header</strong> based on <a href="https://code.djangoproject.com/browser/django/trunk/django/core/mail/message.py" rel="noreferrer">https://code.djangoproject.com/browser/django/trunk/django/core/mail/message.py</a></li> </ol> <p>Here is an example:</p> <pre><code># Custom middleware class MyErrorMiddleware(object): def process_response(self, request, response): if response.status_code == 404: domain = request.get_host() referer = request.META.get('HTTP_REFERER', None) is_internal = _is_internal_request(domain, referer) path = request.get_full_path() if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer): ua = request.META.get('HTTP_USER_AGENT', '&lt;none&gt;') ip = request.META.get('REMOTE_ADDR', '&lt;none&gt;') mail_error("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain), "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \ % (referer, request.get_full_path(), ua, ip), fail_silently=True) return response # Custom mail_error function def mail_error(subject, message, fail_silently=False, connection=None, html_message=None): """Sends a message to the managers, as defined by the MANAGERS setting.""" if not settings.MANAGERS: return # put your extra headers here mail = EmailMultiAlternatives(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message, settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS], connection=connection, header={}) mail if html_message: mail.attach_alternative(html_message, 'text/html') mail.send(fail_silently=fail_silently) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload