Skip to content

Commit

Permalink
Fixed #15603 -- Changed the traceback error e-mails not to use HTML b…
Browse files Browse the repository at this point in the history
…y default. It's now configurable with an 'include_html' parameter to AdminEmailHandler. Thanks, kmtracey

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15850 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Mar 16, 2011
1 parent 2ee7cfc commit 304a50d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 6 additions & 2 deletions django/utils/log.py
Expand Up @@ -48,7 +48,11 @@ def getLogger(name=None):
logger.addHandler(NullHandler()) logger.addHandler(NullHandler())


class AdminEmailHandler(logging.Handler): class AdminEmailHandler(logging.Handler):
"""An exception log handler that emails log entries to site admins def __init__(self, include_html=False):
logging.Handler.__init__(self)
self.include_html = include_html

"""An exception log handler that e-mails log entries to site admins.
If the request is passed as the first argument to the log record, If the request is passed as the first argument to the log record,
request data will be provided in the request data will be provided in the
Expand Down Expand Up @@ -88,6 +92,6 @@ def emit(self, record):


message = "%s\n\n%s" % (stack_trace, request_repr) message = "%s\n\n%s" % (stack_trace, request_repr)
reporter = ExceptionReporter(request, is_email=True, *exc_info) reporter = ExceptionReporter(request, is_email=True, *exc_info)
html_message = reporter.get_traceback_html() html_message = self.include_html and reporter.get_traceback_html() or None
mail.mail_admins(subject, message, fail_silently=True, mail.mail_admins(subject, message, fail_silently=True,
html_message=html_message) html_message=html_message)
29 changes: 27 additions & 2 deletions docs/topics/logging.txt
Expand Up @@ -468,13 +468,38 @@ Handlers
Django provides one log handler in addition to those provided by the Django provides one log handler in addition to those provided by the
Python logging module. Python logging module.


.. class:: AdminEmailHandler() .. class:: AdminEmailHandler([include_html=False])


This handler sends an e-mail to the site admins for each log This handler sends an e-mail to the site admins for each log
message it receives. message it receives.


If the log record contains a 'request' attribute, the full details If the log record contains a ``request`` attribute, the full details
of the request will be included in the e-mail. of the request will be included in the e-mail.


If the log record contains stack trace information, that stack If the log record contains stack trace information, that stack
trace will be included in the e-mail. trace will be included in the e-mail.

The ``include_html`` argument of ``AdminEmailHandler`` is used to
control whether the traceback e-mail includes an HTML attachment
containing the full content of the debug Web page that would have been
produced if ``DEBUG`` were ``True``. To set this value in your
configuration, include it in the handler definition for
``django.utils.log.AdminEmailHandler``, like this::

'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
}
},

Note that this HTML version of the e-mail contains a full traceback,
with names and values of local variables at each level of the stack, plus
the values of your Django settings. This information is potentially very
sensitive, and you may not want to send it over e-mail. Consider using
something such as `django-sentry`_ to get the best of both worlds -- the
rich information of full tracebacks plus the security of *not* sending the
information over e-mail.

.. _django-sentry: http://pypi.python.org/pypi/django-sentry

0 comments on commit 304a50d

Please sign in to comment.