1. .. _logging-ref:
    
  2. 
    
  3. =======
    
  4. Logging
    
  5. =======
    
  6. 
    
  7. .. seealso::
    
  8. 
    
  9.     * :ref:`logging-how-to`
    
  10.     * :ref:`Django logging overview <logging-explanation>`
    
  11. 
    
  12. .. module:: django.utils.log
    
  13.    :synopsis: Logging tools for Django applications
    
  14. 
    
  15. Django's logging module extends Python's builtin :mod:`logging`.
    
  16. 
    
  17. Logging is configured as part of the general Django :func:`django.setup`
    
  18. function, so it's always available unless explicitly disabled.
    
  19. 
    
  20. .. _default-logging-configuration:
    
  21. 
    
  22. Django's default logging configuration
    
  23. ======================================
    
  24. 
    
  25. By default, Django uses Python's :ref:`logging.config.dictConfig format
    
  26. <logging-config-dictschema>`.
    
  27. 
    
  28. Default logging conditions
    
  29. --------------------------
    
  30. 
    
  31. The full set of default logging conditions are:
    
  32. 
    
  33. When :setting:`DEBUG` is ``True``:
    
  34. 
    
  35. * The ``django`` logger sends messages in the ``django`` hierarchy (except
    
  36.   ``django.server``) at the ``INFO`` level or higher to the console.
    
  37. 
    
  38. When :setting:`DEBUG` is ``False``:
    
  39. 
    
  40. * The ``django`` logger sends messages in the ``django`` hierarchy (except
    
  41.   ``django.server``)  with ``ERROR`` or ``CRITICAL`` level to
    
  42.   :class:`AdminEmailHandler`.
    
  43. 
    
  44. Independently of the value of :setting:`DEBUG`:
    
  45. 
    
  46. * The :ref:`django-server-logger` logger sends messages at the ``INFO`` level
    
  47.   or higher to the console.
    
  48. 
    
  49. All loggers except :ref:`django-server-logger` propagate logging to their
    
  50. parents, up to the root ``django`` logger. The ``console`` and ``mail_admins``
    
  51. handlers are attached to the root logger to provide the behavior described
    
  52. above.
    
  53. 
    
  54. Python's own defaults send records of level ``WARNING`` and higher
    
  55. to the console.
    
  56. 
    
  57. .. _default-logging-definition:
    
  58. 
    
  59. Default logging definition
    
  60. --------------------------
    
  61. 
    
  62. Django's default logging configuration inherits Python's defaults. It's
    
  63. available as ``django.utils.log.DEFAULT_LOGGING`` and defined in
    
  64. :source:`django/utils/log.py`::
    
  65. 
    
  66.     {
    
  67.         'version': 1,
    
  68.         'disable_existing_loggers': False,
    
  69.         'filters': {
    
  70.             'require_debug_false': {
    
  71.                 '()': 'django.utils.log.RequireDebugFalse',
    
  72.             },
    
  73.             'require_debug_true': {
    
  74.                 '()': 'django.utils.log.RequireDebugTrue',
    
  75.             },
    
  76.         },
    
  77.         'formatters': {
    
  78.             'django.server': {
    
  79.                 '()': 'django.utils.log.ServerFormatter',
    
  80.                 'format': '[{server_time}] {message}',
    
  81.                 'style': '{',
    
  82.             }
    
  83.         },
    
  84.         'handlers': {
    
  85.             'console': {
    
  86.                 'level': 'INFO',
    
  87.                 'filters': ['require_debug_true'],
    
  88.                 'class': 'logging.StreamHandler',
    
  89.             },
    
  90.             'django.server': {
    
  91.                 'level': 'INFO',
    
  92.                 'class': 'logging.StreamHandler',
    
  93.                 'formatter': 'django.server',
    
  94.             },
    
  95.             'mail_admins': {
    
  96.                 'level': 'ERROR',
    
  97.                 'filters': ['require_debug_false'],
    
  98.                 'class': 'django.utils.log.AdminEmailHandler'
    
  99.             }
    
  100.         },
    
  101.         'loggers': {
    
  102.             'django': {
    
  103.                 'handlers': ['console', 'mail_admins'],
    
  104.                 'level': 'INFO',
    
  105.             },
    
  106.             'django.server': {
    
  107.                 'handlers': ['django.server'],
    
  108.                 'level': 'INFO',
    
  109.                 'propagate': False,
    
  110.             },
    
  111.         }
    
  112.     }
    
  113. 
    
  114. See :ref:`configuring-logging` on how to complement or replace this default
    
  115. logging configuration.
    
  116. 
    
  117. Django logging extensions
    
  118. =========================
    
  119. 
    
  120. Django provides a number of utilities to handle the particular requirements of
    
  121. logging in a web server environment.
    
  122. 
    
  123. Loggers
    
  124. -------
    
  125. 
    
  126. Django provides several built-in loggers.
    
  127. 
    
  128. .. _django-logger:
    
  129. 
    
  130. ``django``
    
  131. ~~~~~~~~~~
    
  132. 
    
  133. The parent logger for messages in the ``django`` :ref:`named logger hierarchy
    
  134. <naming-loggers-hierarchy>`. Django does not post messages using this name.
    
  135. Instead, it uses one of the loggers below.
    
  136. 
    
  137. .. _django-request-logger:
    
  138. 
    
  139. ``django.request``
    
  140. ~~~~~~~~~~~~~~~~~~
    
  141. 
    
  142. Log messages related to the handling of requests. 5XX responses are
    
  143. raised as ``ERROR`` messages; 4XX responses are raised as ``WARNING``
    
  144. messages. Requests that are logged to the ``django.security`` logger aren't
    
  145. logged to ``django.request``.
    
  146. 
    
  147. Messages to this logger have the following extra context:
    
  148. 
    
  149. * ``status_code``: The HTTP response code associated with the request.
    
  150. 
    
  151. * ``request``: The request object that generated the logging message.
    
  152. 
    
  153. .. _django-server-logger:
    
  154. 
    
  155. ``django.server``
    
  156. ~~~~~~~~~~~~~~~~~
    
  157. 
    
  158. Log messages related to the handling of requests received by the server invoked
    
  159. by the :djadmin:`runserver` command. HTTP 5XX responses are logged as ``ERROR``
    
  160. messages, 4XX responses are logged as ``WARNING`` messages, and everything else
    
  161. is logged as ``INFO``.
    
  162. 
    
  163. Messages to this logger have the following extra context:
    
  164. 
    
  165. * ``status_code``: The HTTP response code associated with the request.
    
  166. 
    
  167. * ``request``: The request object that generated the logging message.
    
  168. 
    
  169. .. _django-template-logger:
    
  170. 
    
  171. ``django.template``
    
  172. ~~~~~~~~~~~~~~~~~~~
    
  173. 
    
  174. Log messages related to the rendering of templates.
    
  175. 
    
  176. * Missing context variables are logged as ``DEBUG`` messages.
    
  177. 
    
  178. .. _django-db-logger:
    
  179. 
    
  180. ``django.db.backends``
    
  181. ~~~~~~~~~~~~~~~~~~~~~~
    
  182. 
    
  183. Messages relating to the interaction of code with the database. For example,
    
  184. every application-level SQL statement executed by a request is logged at the
    
  185. ``DEBUG`` level to this logger.
    
  186. 
    
  187. Messages to this logger have the following extra context:
    
  188. 
    
  189. * ``duration``: The time taken to execute the SQL statement.
    
  190. * ``sql``: The SQL statement that was executed.
    
  191. * ``params``: The parameters that were used in the SQL call.
    
  192. * ``alias``: The alias of the database used in the SQL call.
    
  193. 
    
  194. For performance reasons, SQL logging is only enabled when
    
  195. ``settings.DEBUG`` is set to ``True``, regardless of the logging
    
  196. level or handlers that are installed.
    
  197. 
    
  198. This logging does not include framework-level initialization (e.g.
    
  199. ``SET TIMEZONE``) or transaction management queries (e.g. ``BEGIN``,
    
  200. ``COMMIT``, and ``ROLLBACK``). Turn on query logging in your database if you
    
  201. wish to view all database queries.
    
  202. 
    
  203. .. versionchanged:: 4.0
    
  204. 
    
  205.     The database ``alias`` was added to log messages.
    
  206. 
    
  207. .. _django-security-logger:
    
  208. 
    
  209. ``django.security.*``
    
  210. ~~~~~~~~~~~~~~~~~~~~~
    
  211. 
    
  212. The security loggers will receive messages on any occurrence of
    
  213. :exc:`~django.core.exceptions.SuspiciousOperation` and other security-related
    
  214. errors. There is a sub-logger for each subtype of security error, including all
    
  215. ``SuspiciousOperation``\s. The level of the log event depends on where the
    
  216. exception is handled.  Most occurrences are logged as a warning, while
    
  217. any ``SuspiciousOperation`` that reaches the WSGI handler will be logged as an
    
  218. error. For example, when an HTTP ``Host`` header is included in a request from
    
  219. a client that does not match :setting:`ALLOWED_HOSTS`, Django will return a 400
    
  220. response, and an error message will be logged to the
    
  221. ``django.security.DisallowedHost`` logger.
    
  222. 
    
  223. These log events will reach the ``django`` logger by default, which mails error
    
  224. events to admins when ``DEBUG=False``. Requests resulting in a 400 response due
    
  225. to a ``SuspiciousOperation`` will not be logged to the ``django.request``
    
  226. logger, but only to the ``django.security`` logger.
    
  227. 
    
  228. To silence a particular type of ``SuspiciousOperation``, you can override that
    
  229. specific logger following this example::
    
  230. 
    
  231.     'handlers': {
    
  232.         'null': {
    
  233.             'class': 'logging.NullHandler',
    
  234.         },
    
  235.     },
    
  236.     'loggers': {
    
  237.         'django.security.DisallowedHost': {
    
  238.             'handlers': ['null'],
    
  239.             'propagate': False,
    
  240.         },
    
  241.     },
    
  242. 
    
  243. Other ``django.security`` loggers not based on ``SuspiciousOperation`` are:
    
  244. 
    
  245. * ``django.security.csrf``: For :ref:`CSRF failures <csrf-rejected-requests>`.
    
  246. 
    
  247. ``django.db.backends.schema``
    
  248. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  249. 
    
  250. Logs the SQL queries that are executed during schema changes to the database by
    
  251. the :doc:`migrations framework </topics/migrations>`. Note that it won't log the
    
  252. queries executed by :class:`~django.db.migrations.operations.RunPython`.
    
  253. Messages to this logger have ``params`` and ``sql`` in their extra context (but
    
  254. unlike ``django.db.backends``, not duration). The values have the same meaning
    
  255. as explained in :ref:`django-db-logger`.
    
  256. 
    
  257. Handlers
    
  258. --------
    
  259. 
    
  260. Django provides one log handler in addition to :mod:`those provided by the
    
  261. Python logging module <python:logging.handlers>`.
    
  262. 
    
  263. .. class:: AdminEmailHandler(include_html=False, email_backend=None, reporter_class=None)
    
  264. 
    
  265.     This handler sends an email to the site :setting:`ADMINS` for each log
    
  266.     message it receives.
    
  267. 
    
  268.     If the log record contains a ``request`` attribute, the full details
    
  269.     of the request will be included in the email. The email subject will
    
  270.     include the phrase "internal IP" if the client's IP address is in the
    
  271.     :setting:`INTERNAL_IPS` setting; if not, it will include "EXTERNAL IP".
    
  272. 
    
  273.     If the log record contains stack trace information, that stack
    
  274.     trace will be included in the email.
    
  275. 
    
  276.     The ``include_html`` argument of ``AdminEmailHandler`` is used to
    
  277.     control whether the traceback email includes an HTML attachment
    
  278.     containing the full content of the debug web page that would have been
    
  279.     produced if :setting:`DEBUG` were ``True``. To set this value in your
    
  280.     configuration, include it in the handler definition for
    
  281.     ``django.utils.log.AdminEmailHandler``, like this::
    
  282. 
    
  283.         'handlers': {
    
  284.             'mail_admins': {
    
  285.                 'level': 'ERROR',
    
  286.                 'class': 'django.utils.log.AdminEmailHandler',
    
  287.                 'include_html': True,
    
  288.             },
    
  289.         },
    
  290. 
    
  291.     Be aware of the :ref:`security implications of logging
    
  292.     <logging-security-implications>` when using the ``AdminEmailHandler``.
    
  293. 
    
  294.     By setting the ``email_backend`` argument of ``AdminEmailHandler``, the
    
  295.     :ref:`email backend <topic-email-backends>` that is being used by the
    
  296.     handler can be overridden, like this::
    
  297. 
    
  298.         'handlers': {
    
  299.             'mail_admins': {
    
  300.                 'level': 'ERROR',
    
  301.                 'class': 'django.utils.log.AdminEmailHandler',
    
  302.                 'email_backend': 'django.core.mail.backends.filebased.EmailBackend',
    
  303.             },
    
  304.         },
    
  305. 
    
  306.     By default, an instance of the email backend specified in
    
  307.     :setting:`EMAIL_BACKEND` will be used.
    
  308. 
    
  309.     The ``reporter_class`` argument of ``AdminEmailHandler`` allows providing
    
  310.     an ``django.views.debug.ExceptionReporter`` subclass to customize the
    
  311.     traceback text sent in the email body. You provide a string import path to
    
  312.     the class you wish to use, like this::
    
  313. 
    
  314.         'handlers': {
    
  315.             'mail_admins': {
    
  316.                 'level': 'ERROR',
    
  317.                 'class': 'django.utils.log.AdminEmailHandler',
    
  318.                 'include_html': True,
    
  319.                 'reporter_class': 'somepackage.error_reporter.CustomErrorReporter',
    
  320.             },
    
  321.         },
    
  322. 
    
  323.     .. method:: send_mail(subject, message, *args, **kwargs)
    
  324. 
    
  325.         Sends emails to admin users. To customize this behavior, you can
    
  326.         subclass the :class:`~django.utils.log.AdminEmailHandler` class and
    
  327.         override this method.
    
  328. 
    
  329. Filters
    
  330. -------
    
  331. 
    
  332. Django provides some log filters in addition to those provided by the Python
    
  333. logging module.
    
  334. 
    
  335. .. class:: CallbackFilter(callback)
    
  336. 
    
  337.     This filter accepts a callback function (which should accept a single
    
  338.     argument, the record to be logged), and calls it for each record that
    
  339.     passes through the filter. Handling of that record will not proceed if the
    
  340.     callback returns False.
    
  341. 
    
  342.     For instance, to filter out :exc:`~django.http.UnreadablePostError`
    
  343.     (raised when a user cancels an upload) from the admin emails, you would
    
  344.     create a filter function::
    
  345. 
    
  346.         from django.http import UnreadablePostError
    
  347. 
    
  348.         def skip_unreadable_post(record):
    
  349.             if record.exc_info:
    
  350.                 exc_type, exc_value = record.exc_info[:2]
    
  351.                 if isinstance(exc_value, UnreadablePostError):
    
  352.                     return False
    
  353.             return True
    
  354. 
    
  355.     and then add it to your logging config::
    
  356. 
    
  357.         'filters': {
    
  358.             'skip_unreadable_posts': {
    
  359.                 '()': 'django.utils.log.CallbackFilter',
    
  360.                 'callback': skip_unreadable_post,
    
  361.             },
    
  362.         },
    
  363.         'handlers': {
    
  364.             'mail_admins': {
    
  365.                 'level': 'ERROR',
    
  366.                 'filters': ['skip_unreadable_posts'],
    
  367.                 'class': 'django.utils.log.AdminEmailHandler',
    
  368.             },
    
  369.         },
    
  370. 
    
  371. .. class:: RequireDebugFalse()
    
  372. 
    
  373.     This filter will only pass on records when settings.DEBUG is False.
    
  374. 
    
  375.     This filter is used as follows in the default :setting:`LOGGING`
    
  376.     configuration to ensure that the :class:`AdminEmailHandler` only sends
    
  377.     error emails to admins when :setting:`DEBUG` is ``False``::
    
  378. 
    
  379.         'filters': {
    
  380.             'require_debug_false': {
    
  381.                 '()': 'django.utils.log.RequireDebugFalse',
    
  382.             },
    
  383.         },
    
  384.         'handlers': {
    
  385.             'mail_admins': {
    
  386.                 'level': 'ERROR',
    
  387.                 'filters': ['require_debug_false'],
    
  388.                 'class': 'django.utils.log.AdminEmailHandler',
    
  389.             },
    
  390.         },
    
  391. 
    
  392. .. class:: RequireDebugTrue()
    
  393. 
    
  394.     This filter is similar to :class:`RequireDebugFalse`, except that records are
    
  395.     passed only when :setting:`DEBUG` is ``True``.