2011-06-09 00:18:46 +02:00
|
|
|
Error reporting
|
|
|
|
===============
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
When you're running a public site you should always turn off the
|
|
|
|
:setting:`DEBUG` setting. That will make your server run much faster, and will
|
|
|
|
also prevent malicious users from seeing details of your application that can be
|
|
|
|
revealed by the error pages.
|
|
|
|
|
|
|
|
However, running with :setting:`DEBUG` set to ``False`` means you'll never see
|
|
|
|
errors generated by your site -- everyone will just see your public error pages.
|
|
|
|
You need to keep track of errors that occur in deployed sites, so Django can be
|
2011-06-09 00:18:46 +02:00
|
|
|
configured to create reports with details about those errors.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2011-06-09 00:18:46 +02:00
|
|
|
Email reports
|
2008-08-24 00:25:40 +02:00
|
|
|
-------------
|
|
|
|
|
2011-06-09 00:18:46 +02:00
|
|
|
Server errors
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
When :setting:`DEBUG` is ``False``, Django will email the users listed in the
|
2010-03-11 10:33:49 +01:00
|
|
|
:setting:`ADMINS` setting whenever your code raises an unhandled exception and
|
2008-08-24 00:25:40 +02:00
|
|
|
results in an internal server error (HTTP status code 500). This gives the
|
|
|
|
administrators immediate notification of any errors. The :setting:`ADMINS` will
|
|
|
|
get a description of the error, a complete Python traceback, and details about
|
|
|
|
the HTTP request that caused the error.
|
|
|
|
|
2010-03-06 12:20:50 +01:00
|
|
|
.. note::
|
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
In order to send email, Django requires a few settings telling it
|
2010-03-06 12:20:50 +01:00
|
|
|
how to connect to your mail server. At the very least, you'll need
|
|
|
|
to specify :setting:`EMAIL_HOST` and possibly
|
|
|
|
:setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD`,
|
|
|
|
though other settings may be also required depending on your mail
|
2010-08-19 21:27:44 +02:00
|
|
|
server's configuration. Consult :doc:`the Django settings
|
|
|
|
documentation </ref/settings>` for a full list of email-related
|
2010-03-06 12:20:50 +01:00
|
|
|
settings.
|
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
By default, Django will send email from root@localhost. However, some mail
|
|
|
|
providers reject all email from this address. To use a different sender
|
2009-07-11 17:37:38 +02:00
|
|
|
address, modify the :setting:`SERVER_EMAIL` setting.
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
To disable this behavior, just remove all entries from the :setting:`ADMINS`
|
|
|
|
setting.
|
|
|
|
|
2010-11-16 15:00:38 +01:00
|
|
|
.. seealso::
|
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
Server error emails are sent using the logging framework, so you can
|
2011-05-13 06:33:42 +02:00
|
|
|
customize this behavior by :doc:`customizing your logging configuration
|
2010-11-16 15:00:38 +01:00
|
|
|
</topics/logging>`.
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
404 errors
|
2011-06-09 00:18:46 +02:00
|
|
|
~~~~~~~~~~
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
Django can also be configured to email errors about broken links (404 "page
|
|
|
|
not found" errors). Django sends emails about 404 errors when:
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-01-01 22:28:48 +01:00
|
|
|
* :setting:`DEBUG` is ``False``;
|
2009-07-11 17:37:38 +02:00
|
|
|
|
2013-01-01 22:28:48 +01:00
|
|
|
* Your :setting:`MIDDLEWARE_CLASSES` setting includes
|
|
|
|
:class:`django.middleware.common.BrokenLinkEmailsMiddleware`.
|
2009-07-11 17:37:38 +02:00
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
If those conditions are met, Django will email the users listed in the
|
2008-08-24 00:25:40 +02:00
|
|
|
:setting:`MANAGERS` setting whenever your code raises a 404 and the request has
|
2011-04-01 18:10:22 +02:00
|
|
|
a referer. (It doesn't bother to email for 404s that don't have a referer --
|
2010-10-09 10:12:50 +02:00
|
|
|
those are usually just people typing in broken URLs or broken Web 'bots).
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-01-01 22:28:48 +01:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
:class:`~django.middleware.common.BrokenLinkEmailsMiddleware` must appear
|
|
|
|
before other middleware that intercepts 404 errors, such as
|
|
|
|
:class:`~django.middleware.locale.LocaleMiddleware` or
|
|
|
|
:class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware`.
|
|
|
|
Put it towards the top of your :setting:`MIDDLEWARE_CLASSES` setting.
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
You can tell Django to stop reporting particular 404s by tweaking the
|
2011-05-05 22:49:26 +02:00
|
|
|
:setting:`IGNORABLE_404_URLS` setting. It should be a tuple of compiled
|
|
|
|
regular expression objects. For example::
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2011-05-05 22:49:26 +02:00
|
|
|
import re
|
|
|
|
IGNORABLE_404_URLS = (
|
|
|
|
re.compile(r'\.(php|cgi)$'),
|
|
|
|
re.compile(r'^/phpmyadmin/'),
|
|
|
|
)
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
In this example, a 404 to any URL ending with ``.php`` or ``.cgi`` will *not* be
|
|
|
|
reported. Neither will any URL starting with ``/phpmyadmin/``.
|
|
|
|
|
2011-05-05 22:49:26 +02:00
|
|
|
The following example shows how to exclude some conventional URLs that browsers and
|
|
|
|
crawlers often request::
|
|
|
|
|
|
|
|
import re
|
|
|
|
IGNORABLE_404_URLS = (
|
|
|
|
re.compile(r'^/apple-touch-icon.*\.png$'),
|
2011-11-28 20:41:58 +01:00
|
|
|
re.compile(r'^/favicon\.ico$'),
|
|
|
|
re.compile(r'^/robots\.txt$'),
|
2011-05-05 22:49:26 +02:00
|
|
|
)
|
|
|
|
|
2011-05-24 20:39:28 +02:00
|
|
|
(Note that these are regular expressions, so we put a backslash in front of
|
|
|
|
periods to escape them.)
|
2011-05-05 22:49:26 +02:00
|
|
|
|
2010-11-16 15:00:38 +01:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
404 errors are logged using the logging framework. By default, these log
|
|
|
|
records are ignored, but you can use them for error reporting by writing a
|
|
|
|
handler and :doc:`configuring logging </topics/logging>` appropriately.
|
|
|
|
|
2011-06-09 00:18:46 +02:00
|
|
|
.. _filtering-error-reports:
|
|
|
|
|
|
|
|
Filtering error reports
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
Filtering sensitive information
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Error reports are really helpful for debugging errors, so it is generally
|
|
|
|
useful to record as much relevant information about those errors as possible.
|
|
|
|
For example, by default Django records the `full traceback`_ for the
|
|
|
|
exception raised, each `traceback frame`_'s local variables, and the
|
2012-12-25 15:56:22 +01:00
|
|
|
:class:`~django.http.HttpRequest`'s :ref:`attributes<httprequest-attributes>`.
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
However, sometimes certain types of information may be too sensitive and thus
|
|
|
|
may not be appropriate to be kept track of, for example a user's password or
|
|
|
|
credit card number. So Django offers a set of function decorators to help you
|
|
|
|
control which information should be filtered out of error reports in a
|
|
|
|
production environment (that is, where :setting:`DEBUG` is set to ``False``):
|
|
|
|
:func:`sensitive_variables` and :func:`sensitive_post_parameters`.
|
|
|
|
|
|
|
|
.. _`full traceback`: http://en.wikipedia.org/wiki/Stack_trace
|
|
|
|
.. _`traceback frame`: http://en.wikipedia.org/wiki/Stack_frame
|
|
|
|
|
|
|
|
.. function:: sensitive_variables(*variables)
|
|
|
|
|
|
|
|
If a function (either a view or any regular callback) in your code uses
|
|
|
|
local variables susceptible to contain sensitive information, you may
|
|
|
|
prevent the values of those variables from being included in error reports
|
2011-10-14 02:12:01 +02:00
|
|
|
using the ``sensitive_variables`` decorator::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
from django.views.decorators.debug import sensitive_variables
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
@sensitive_variables('user', 'pw', 'cc')
|
|
|
|
def process_info(user):
|
|
|
|
pw = user.pass_word
|
|
|
|
cc = user.credit_card_number
|
|
|
|
name = user.name
|
|
|
|
...
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
In the above example, the values for the ``user``, ``pw`` and ``cc``
|
|
|
|
variables will be hidden and replaced with stars (`**********`) in the
|
|
|
|
error reports, whereas the value of the ``name`` variable will be
|
|
|
|
disclosed.
|
|
|
|
|
|
|
|
To systematically hide all local variables of a function from error logs,
|
2011-10-14 02:12:01 +02:00
|
|
|
do not provide any argument to the ``sensitive_variables`` decorator::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
@sensitive_variables()
|
|
|
|
def my_function():
|
|
|
|
...
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2012-12-31 18:34:08 +01:00
|
|
|
.. admonition:: When using mutiple decorators
|
|
|
|
|
|
|
|
If the variable you want to hide is also a function argument (e.g.
|
|
|
|
'``user``' in the following example), and if the decorated function has
|
2013-01-15 16:55:13 +01:00
|
|
|
mutiple decorators, then make sure to place ``@sensitive_variables`` at
|
2012-12-31 18:34:08 +01:00
|
|
|
the top of the decorator chain. This way it will also hide the function
|
|
|
|
argument as it gets passed through the other decorators::
|
|
|
|
|
|
|
|
@sensitive_variables('user', 'pw', 'cc')
|
|
|
|
@some_decorator
|
|
|
|
@another_decorator
|
|
|
|
def process_info(user):
|
|
|
|
...
|
|
|
|
|
2011-06-09 00:18:46 +02:00
|
|
|
.. function:: sensitive_post_parameters(*parameters)
|
|
|
|
|
2012-12-25 15:56:22 +01:00
|
|
|
If one of your views receives an :class:`~django.http.HttpRequest` object
|
|
|
|
with :attr:`POST parameters<django.http.HttpRequest.POST>` susceptible to
|
|
|
|
contain sensitive information, you may prevent the values of those
|
|
|
|
parameters from being included in the error reports using the
|
|
|
|
``sensitive_post_parameters`` decorator::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
@sensitive_post_parameters('pass_word', 'credit_card_number')
|
|
|
|
def record_user_profile(request):
|
|
|
|
UserProfile.create(user=request.user,
|
|
|
|
password=request.POST['pass_word'],
|
|
|
|
credit_card=request.POST['credit_card_number'],
|
|
|
|
name=request.POST['name'])
|
|
|
|
...
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
In the above example, the values for the ``pass_word`` and
|
|
|
|
``credit_card_number`` POST parameters will be hidden and replaced with
|
|
|
|
stars (`**********`) in the request's representation inside the error
|
|
|
|
reports, whereas the value of the ``name`` parameter will be disclosed.
|
|
|
|
|
|
|
|
To systematically hide all POST parameters of a request in error reports,
|
2011-10-14 02:12:01 +02:00
|
|
|
do not provide any argument to the ``sensitive_post_parameters`` decorator::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
@sensitive_post_parameters()
|
|
|
|
def my_view(request):
|
|
|
|
...
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2012-12-26 21:47:29 +01:00
|
|
|
All POST parameters are systematically filtered out of error reports for
|
|
|
|
certain :mod:`django.contrib.auth.views` views (``login``,
|
|
|
|
``password_reset_confirm``, ``password_change``, and ``add_view`` and
|
|
|
|
``user_change_password`` in the ``auth`` admin) to prevent the leaking of
|
|
|
|
sensitive information such as user passwords.
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
.. _custom-error-reports:
|
|
|
|
|
|
|
|
Custom error reports
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
All :func:`sensitive_variables` and :func:`sensitive_post_parameters` do is,
|
|
|
|
respectively, annotate the decorated function with the names of sensitive
|
|
|
|
variables and annotate the ``HttpRequest`` object with the names of sensitive
|
|
|
|
POST parameters, so that this sensitive information can later be filtered out
|
|
|
|
of reports when an error occurs. The actual filtering is done by Django's
|
|
|
|
default error reporter filter:
|
|
|
|
:class:`django.views.debug.SafeExceptionReporterFilter`. This filter uses the
|
|
|
|
decorators' annotations to replace the corresponding values with stars
|
|
|
|
(`**********`) when the error reports are produced. If you wish to override or
|
|
|
|
customize this default behavior for your entire site, you need to define your
|
|
|
|
own filter class and tell Django to use it via the
|
2011-10-14 02:12:01 +02:00
|
|
|
:setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` setting::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter'
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
You may also control in a more granular way which filter to use within any
|
|
|
|
given view by setting the ``HttpRequest``'s ``exception_reporter_filter``
|
2011-10-14 02:12:01 +02:00
|
|
|
attribute::
|
2011-06-09 00:18:46 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
def my_view(request):
|
|
|
|
if request.user.is_authenticated():
|
|
|
|
request.exception_reporter_filter = CustomExceptionReporterFilter()
|
|
|
|
...
|
2011-06-09 00:18:46 +02:00
|
|
|
|
|
|
|
Your custom filter class needs to inherit from
|
|
|
|
:class:`django.views.debug.SafeExceptionReporterFilter` and may override the
|
|
|
|
following methods:
|
|
|
|
|
|
|
|
.. class:: django.views.debug.SafeExceptionReporterFilter
|
|
|
|
|
|
|
|
.. method:: SafeExceptionReporterFilter.is_active(self, request)
|
|
|
|
|
|
|
|
Returns ``True`` to activate the filtering operated in the other methods.
|
|
|
|
By default the filter is active if :setting:`DEBUG` is ``False``.
|
|
|
|
|
|
|
|
.. method:: SafeExceptionReporterFilter.get_request_repr(self, request)
|
|
|
|
|
|
|
|
Returns the representation string of the request object, that is, the
|
|
|
|
value that would be returned by ``repr(request)``, except it uses the
|
|
|
|
filtered dictionary of POST parameters as determined by
|
|
|
|
:meth:`SafeExceptionReporterFilter.get_post_parameters`.
|
|
|
|
|
|
|
|
.. method:: SafeExceptionReporterFilter.get_post_parameters(self, request)
|
|
|
|
|
|
|
|
Returns the filtered dictionary of POST parameters. By default it replaces
|
|
|
|
the values of sensitive parameters with stars (`**********`).
|
|
|
|
|
|
|
|
.. method:: SafeExceptionReporterFilter.get_traceback_frame_variables(self, request, tb_frame)
|
|
|
|
|
|
|
|
Returns the filtered dictionary of local variables for the given traceback
|
|
|
|
frame. By default it replaces the values of sensitive variables with stars
|
|
|
|
(`**********`).
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
You can also set up custom error reporting by writing a custom piece of
|
|
|
|
:ref:`exception middleware <exception-middleware>`. If you do write custom
|
|
|
|
error handling, it's a good idea to emulate Django's built-in error handling
|
|
|
|
and only report/log errors if :setting:`DEBUG` is ``False``.
|