PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/django-1.4/docs/howto/error-reporting.txt

https://github.com/Tagtoo/GoogleAppEngine
Plain Text | 277 lines | 202 code | 75 blank | 0 comment | 0 complexity | 20bde7b2cf14f48cd794e88ed09dc901 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-2.0, LGPL-2.1, MIT
  1. Error reporting
  2. ===============
  3. When you're running a public site you should always turn off the
  4. :setting:`DEBUG` setting. That will make your server run much faster, and will
  5. also prevent malicious users from seeing details of your application that can be
  6. revealed by the error pages.
  7. However, running with :setting:`DEBUG` set to ``False`` means you'll never see
  8. errors generated by your site -- everyone will just see your public error pages.
  9. You need to keep track of errors that occur in deployed sites, so Django can be
  10. configured to create reports with details about those errors.
  11. Email reports
  12. -------------
  13. Server errors
  14. ~~~~~~~~~~~~~
  15. When :setting:`DEBUG` is ``False``, Django will email the users listed in the
  16. :setting:`ADMINS` setting whenever your code raises an unhandled exception and
  17. results in an internal server error (HTTP status code 500). This gives the
  18. administrators immediate notification of any errors. The :setting:`ADMINS` will
  19. get a description of the error, a complete Python traceback, and details about
  20. the HTTP request that caused the error.
  21. .. note::
  22. In order to send email, Django requires a few settings telling it
  23. how to connect to your mail server. At the very least, you'll need
  24. to specify :setting:`EMAIL_HOST` and possibly
  25. :setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD`,
  26. though other settings may be also required depending on your mail
  27. server's configuration. Consult :doc:`the Django settings
  28. documentation </ref/settings>` for a full list of email-related
  29. settings.
  30. By default, Django will send email from root@localhost. However, some mail
  31. providers reject all email from this address. To use a different sender
  32. address, modify the :setting:`SERVER_EMAIL` setting.
  33. To disable this behavior, just remove all entries from the :setting:`ADMINS`
  34. setting.
  35. .. seealso::
  36. .. versionadded:: 1.3
  37. Server error emails are sent using the logging framework, so you can
  38. customize this behavior by :doc:`customizing your logging configuration
  39. </topics/logging>`.
  40. 404 errors
  41. ~~~~~~~~~~
  42. Django can also be configured to email errors about broken links (404 "page
  43. not found" errors). Django sends emails about 404 errors when:
  44. * :setting:`DEBUG` is ``False``
  45. * :setting:`SEND_BROKEN_LINK_EMAILS` is ``True``
  46. * Your :setting:`MIDDLEWARE_CLASSES` setting includes ``CommonMiddleware``
  47. (which it does by default).
  48. If those conditions are met, Django will email the users listed in the
  49. :setting:`MANAGERS` setting whenever your code raises a 404 and the request has
  50. a referer. (It doesn't bother to email for 404s that don't have a referer --
  51. those are usually just people typing in broken URLs or broken Web 'bots).
  52. You can tell Django to stop reporting particular 404s by tweaking the
  53. :setting:`IGNORABLE_404_URLS` setting. It should be a tuple of compiled
  54. regular expression objects. For example::
  55. import re
  56. IGNORABLE_404_URLS = (
  57. re.compile(r'\.(php|cgi)$'),
  58. re.compile(r'^/phpmyadmin/'),
  59. )
  60. In this example, a 404 to any URL ending with ``.php`` or ``.cgi`` will *not* be
  61. reported. Neither will any URL starting with ``/phpmyadmin/``.
  62. The following example shows how to exclude some conventional URLs that browsers and
  63. crawlers often request::
  64. import re
  65. IGNORABLE_404_URLS = (
  66. re.compile(r'^/apple-touch-icon.*\.png$'),
  67. re.compile(r'^/favicon\.ico$'),
  68. re.compile(r'^/robots\.txt$'),
  69. )
  70. (Note that these are regular expressions, so we put a backslash in front of
  71. periods to escape them.)
  72. The best way to disable this behavior is to set
  73. :setting:`SEND_BROKEN_LINK_EMAILS` to ``False``.
  74. .. seealso::
  75. .. versionadded:: 1.3
  76. 404 errors are logged using the logging framework. By default, these log
  77. records are ignored, but you can use them for error reporting by writing a
  78. handler and :doc:`configuring logging </topics/logging>` appropriately.
  79. .. seealso::
  80. .. versionchanged:: 1.4
  81. Previously, two settings were used to control which URLs not to report:
  82. :setting:`IGNORABLE_404_STARTS` and :setting:`IGNORABLE_404_ENDS`. They
  83. were replaced by :setting:`IGNORABLE_404_URLS`.
  84. .. _filtering-error-reports:
  85. Filtering error reports
  86. -----------------------
  87. .. versionadded:: 1.4
  88. Filtering sensitive information
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. .. currentmodule:: django.views.decorators.debug
  91. Error reports are really helpful for debugging errors, so it is generally
  92. useful to record as much relevant information about those errors as possible.
  93. For example, by default Django records the `full traceback`_ for the
  94. exception raised, each `traceback frame`_'s local variables, and the
  95. :class:`HttpRequest`'s :ref:`attributes<httprequest-attributes>`.
  96. However, sometimes certain types of information may be too sensitive and thus
  97. may not be appropriate to be kept track of, for example a user's password or
  98. credit card number. So Django offers a set of function decorators to help you
  99. control which information should be filtered out of error reports in a
  100. production environment (that is, where :setting:`DEBUG` is set to ``False``):
  101. :func:`sensitive_variables` and :func:`sensitive_post_parameters`.
  102. .. _`full traceback`: http://en.wikipedia.org/wiki/Stack_trace
  103. .. _`traceback frame`: http://en.wikipedia.org/wiki/Stack_frame
  104. .. function:: sensitive_variables(*variables)
  105. If a function (either a view or any regular callback) in your code uses
  106. local variables susceptible to contain sensitive information, you may
  107. prevent the values of those variables from being included in error reports
  108. using the ``sensitive_variables`` decorator::
  109. from django.views.decorators.debug import sensitive_variables
  110. @sensitive_variables('user', 'pw', 'cc')
  111. def process_info(user):
  112. pw = user.pass_word
  113. cc = user.credit_card_number
  114. name = user.name
  115. ...
  116. In the above example, the values for the ``user``, ``pw`` and ``cc``
  117. variables will be hidden and replaced with stars (`**********`) in the
  118. error reports, whereas the value of the ``name`` variable will be
  119. disclosed.
  120. To systematically hide all local variables of a function from error logs,
  121. do not provide any argument to the ``sensitive_variables`` decorator::
  122. @sensitive_variables()
  123. def my_function():
  124. ...
  125. .. function:: sensitive_post_parameters(*parameters)
  126. If one of your views receives an :class:`HttpRequest` object with
  127. :attr:`POST parameters<HttpRequest.POST>` susceptible to contain sensitive
  128. information, you may prevent the values of those parameters from being
  129. included in the error reports using the ``sensitive_post_parameters``
  130. decorator::
  131. from django.views.decorators.debug import sensitive_post_parameters
  132. @sensitive_post_parameters('pass_word', 'credit_card_number')
  133. def record_user_profile(request):
  134. UserProfile.create(user=request.user,
  135. password=request.POST['pass_word'],
  136. credit_card=request.POST['credit_card_number'],
  137. name=request.POST['name'])
  138. ...
  139. In the above example, the values for the ``pass_word`` and
  140. ``credit_card_number`` POST parameters will be hidden and replaced with
  141. stars (`**********`) in the request's representation inside the error
  142. reports, whereas the value of the ``name`` parameter will be disclosed.
  143. To systematically hide all POST parameters of a request in error reports,
  144. do not provide any argument to the ``sensitive_post_parameters`` decorator::
  145. @sensitive_post_parameters()
  146. def my_view(request):
  147. ...
  148. .. note::
  149. .. versionchanged:: 1.4
  150. Since version 1.4, all POST parameters are systematically filtered out of
  151. error reports for certain :mod:`contrib.views.auth` views (``login``,
  152. ``password_reset_confirm``, ``password_change``, and ``add_view`` and
  153. ``user_change_password`` in the ``auth`` admin) to prevent the leaking of
  154. sensitive information such as user passwords.
  155. .. _custom-error-reports:
  156. Custom error reports
  157. ~~~~~~~~~~~~~~~~~~~~
  158. All :func:`sensitive_variables` and :func:`sensitive_post_parameters` do is,
  159. respectively, annotate the decorated function with the names of sensitive
  160. variables and annotate the ``HttpRequest`` object with the names of sensitive
  161. POST parameters, so that this sensitive information can later be filtered out
  162. of reports when an error occurs. The actual filtering is done by Django's
  163. default error reporter filter:
  164. :class:`django.views.debug.SafeExceptionReporterFilter`. This filter uses the
  165. decorators' annotations to replace the corresponding values with stars
  166. (`**********`) when the error reports are produced. If you wish to override or
  167. customize this default behavior for your entire site, you need to define your
  168. own filter class and tell Django to use it via the
  169. :setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` setting::
  170. DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter'
  171. You may also control in a more granular way which filter to use within any
  172. given view by setting the ``HttpRequest``'s ``exception_reporter_filter``
  173. attribute::
  174. def my_view(request):
  175. if request.user.is_authenticated():
  176. request.exception_reporter_filter = CustomExceptionReporterFilter()
  177. ...
  178. .. currentmodule:: django.views.debug
  179. Your custom filter class needs to inherit from
  180. :class:`django.views.debug.SafeExceptionReporterFilter` and may override the
  181. following methods:
  182. .. class:: SafeExceptionReporterFilter
  183. .. method:: SafeExceptionReporterFilter.is_active(self, request)
  184. Returns ``True`` to activate the filtering operated in the other methods.
  185. By default the filter is active if :setting:`DEBUG` is ``False``.
  186. .. method:: SafeExceptionReporterFilter.get_request_repr(self, request)
  187. Returns the representation string of the request object, that is, the
  188. value that would be returned by ``repr(request)``, except it uses the
  189. filtered dictionary of POST parameters as determined by
  190. :meth:`SafeExceptionReporterFilter.get_post_parameters`.
  191. .. method:: SafeExceptionReporterFilter.get_post_parameters(self, request)
  192. Returns the filtered dictionary of POST parameters. By default it replaces
  193. the values of sensitive parameters with stars (`**********`).
  194. .. method:: SafeExceptionReporterFilter.get_traceback_frame_variables(self, request, tb_frame)
  195. Returns the filtered dictionary of local variables for the given traceback
  196. frame. By default it replaces the values of sensitive variables with stars
  197. (`**********`).
  198. .. seealso::
  199. You can also set up custom error reporting by writing a custom piece of
  200. :ref:`exception middleware <exception-middleware>`. If you do write custom
  201. error handling, it's a good idea to emulate Django's built-in error handling
  202. and only report/log errors if :setting:`DEBUG` is ``False``.