logging.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. .. _logging-how-to:
  2. ================================
  3. How to configure and use logging
  4. ================================
  5. .. seealso::
  6. * :ref:`Django logging reference <logging-ref>`
  7. * :ref:`Django logging overview <logging-explanation>`
  8. Django provides a working :ref:`default logging configuration
  9. <default-logging-configuration>` that is readily extended.
  10. Make a basic logging call
  11. =========================
  12. To send a log message from within your code, you place a logging call into it.
  13. .. admonition:: Don't be tempted to use logging calls in ``settings.py``.
  14. The way that Django logging is configured as part of the ``setup()``
  15. function means that logging calls placed in ``settings.py`` may not work as
  16. expected, because *logging will not be set up at that point*. To explore
  17. logging, use a view function as suggested in the example below.
  18. First, import the Python logging library, and then obtain a logger instance
  19. with :py:func:`logging.getLogger`. Provide the ``getLogger()`` method with a
  20. name to identify it and the records it emits. A good option is to use
  21. ``__name__`` (see :ref:`naming-loggers` below for more on this) which will
  22. provide the name of the current Python module as a dotted path::
  23. import logging
  24. logger = logging.getLogger(__name__)
  25. It's a good convention to perform this declaration at module level.
  26. And then in a function, for example in a view, send a record to the logger::
  27. def some_view(request):
  28. ...
  29. if some_risky_state:
  30. logger.warning("Platform is running at risk")
  31. When this code is executed, a :py:class:`~logging.LogRecord` containing that
  32. message will be sent to the logger. If you're using Django's default logging
  33. configuration, the message will appear in the console.
  34. The ``WARNING`` level used in the example above is one of several
  35. :ref:`logging severity levels <topic-logging-parts-loggers>`: ``DEBUG``,
  36. ``INFO``, ``WARNING``, ``ERROR``, ``CRITICAL``. So, another example might be::
  37. logger.critical("Payment system is not responding")
  38. .. important::
  39. Records with a level lower than ``WARNING`` will not appear in the console
  40. by default. Changing this behavior requires additional configuration.
  41. Customize logging configuration
  42. ===============================
  43. Although Django's logging configuration works out of the box, you can control
  44. exactly how your logs are sent to various destinations - to log files, external
  45. services, email and so on - with some additional configuration.
  46. You can configure:
  47. * logger mappings, to determine which records are sent to which handlers
  48. * handlers, to determine what they do with the records they receive
  49. * filters, to provide additional control over the transfer of records, and
  50. even modify records in-place
  51. * formatters, to convert :class:`~logging.LogRecord` objects to a string or
  52. other form for consumption by human beings or another system
  53. There are various ways of configuring logging. In Django, the
  54. :setting:`LOGGING` setting is most commonly used. The setting uses the
  55. :ref:`dictConfig format <logging-config-dictschema>`, and extends the
  56. :ref:`default logging configuration <default-logging-definition>`.
  57. See :ref:`configuring-logging` for an explanation of how your custom settings
  58. are merged with Django's defaults.
  59. See the :mod:`Python logging documentation <python:logging.config>` for
  60. details of other ways of configuring logging. For the sake of simplicity, this
  61. documentation will only consider configuration via the ``LOGGING`` setting.
  62. .. _basic-logger-configuration:
  63. Basic logging configuration
  64. ---------------------------
  65. When configuring logging, it makes sense to
  66. Create a ``LOGGING`` dictionary
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. In your ``settings.py``::
  69. LOGGING = {
  70. "version": 1, # the dictConfig format version
  71. "disable_existing_loggers": False, # retain the default loggers
  72. }
  73. It nearly always makes sense to retain and extend the default logging
  74. configuration by setting ``disable_existing_loggers`` to ``False``.
  75. Configure a handler
  76. ~~~~~~~~~~~~~~~~~~~
  77. This example configures a single handler named ``file``, that uses Python's
  78. :class:`~logging.FileHandler` to save logs of level ``DEBUG`` and higher to the
  79. file ``general.log`` (at the project root):
  80. .. code-block:: python
  81. :emphasize-lines: 3-8
  82. LOGGING = {
  83. # ...
  84. "handlers": {
  85. "file": {
  86. "class": "logging.FileHandler",
  87. "filename": "general.log",
  88. },
  89. },
  90. }
  91. Different handler classes take different configuration options. For more
  92. information on available handler classes, see the
  93. :class:`~django.utils.log.AdminEmailHandler` provided by Django and the various
  94. :py:mod:`handler classes <logging.handlers>` provided by Python.
  95. Logging levels can also be set on the handlers (by default, they accept log
  96. messages of all levels). Using the example above, adding:
  97. .. code-block:: python
  98. :emphasize-lines: 4
  99. {
  100. "class": "logging.FileHandler",
  101. "filename": "general.log",
  102. "level": "DEBUG",
  103. }
  104. would define a handler configuration that only accepts records of level
  105. ``DEBUG`` and higher.
  106. Configure a logger mapping
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  108. To send records to this handler, configure a logger mapping to use it for
  109. example:
  110. .. code-block:: python
  111. :emphasize-lines: 3-8
  112. LOGGING = {
  113. # ...
  114. "loggers": {
  115. "": {
  116. "level": "DEBUG",
  117. "handlers": ["file"],
  118. },
  119. },
  120. }
  121. The mapping's name determines which log records it will process. This
  122. configuration (``''``) is *unnamed*. That means that it will process records
  123. from *all* loggers (see :ref:`naming-loggers` below on how to use the mapping
  124. name to determine the loggers for which it will process records).
  125. It will forward messages of levels ``DEBUG`` and higher to the handler named
  126. ``file``.
  127. Note that a logger can forward messages to multiple handlers, so the relation
  128. between loggers and handlers is many-to-many.
  129. If you execute::
  130. logger.debug("Attempting to connect to API")
  131. in your code, you will find that message in the file ``general.log`` in the
  132. root of the project.
  133. Configure a formatter
  134. ~~~~~~~~~~~~~~~~~~~~~
  135. By default, the final log output contains the message part of each :class:`log
  136. record <logging.LogRecord>`. Use a formatter if you want to include additional
  137. data. First name and define your formatters - this example defines
  138. formatters named ``verbose`` and ``simple``:
  139. .. code-block:: python
  140. :emphasize-lines: 3-12
  141. LOGGING = {
  142. # ...
  143. "formatters": {
  144. "verbose": {
  145. "format": "{name} {levelname} {asctime} {module} {process:d} {thread:d} {message}",
  146. "style": "{",
  147. },
  148. "simple": {
  149. "format": "{levelname} {message}",
  150. "style": "{",
  151. },
  152. },
  153. }
  154. The ``style`` keyword allows you to specify ``{`` for :meth:`str.format` or
  155. ``$`` for :class:`string.Template` formatting; the default is ``$``.
  156. See :ref:`logrecord-attributes` for the :class:`~logging.LogRecord` attributes
  157. you can include.
  158. To apply a formatter to a handler, add a ``formatter`` entry to the handler's
  159. dictionary referring to the formatter by name, for example:
  160. .. code-block:: python
  161. :emphasize-lines: 5
  162. "handlers": {
  163. "file": {
  164. "class": "logging.FileHandler",
  165. "filename": "general.log",
  166. "formatter": "verbose",
  167. },
  168. }
  169. .. _naming-loggers:
  170. Use logger namespacing
  171. ~~~~~~~~~~~~~~~~~~~~~~
  172. The unnamed logging configuration ``''`` captures logs from any Python
  173. application. A named logging configuration will capture logs only from loggers
  174. with matching names.
  175. The namespace of a logger instance is defined using
  176. :py:func:`~logging.getLogger`. For example in ``views.py`` of ``my_app``::
  177. logger = logging.getLogger(__name__)
  178. will create a logger in the ``my_app.views`` namespace. ``__name__`` allows you
  179. to organize log messages according to their provenance within your project's
  180. applications automatically. It also ensures that you will not experience name
  181. collisions.
  182. A logger mapping named ``my_app.views`` will capture records from this logger:
  183. .. code-block:: python
  184. :emphasize-lines: 4
  185. LOGGING = {
  186. # ...
  187. "loggers": {
  188. "my_app.views": {...},
  189. },
  190. }
  191. A logger mapping named ``my_app`` will be more permissive, capturing records
  192. from loggers anywhere within the ``my_app`` namespace (including
  193. ``my_app.views``, ``my_app.utils``, and so on):
  194. .. code-block:: python
  195. :emphasize-lines: 4
  196. LOGGING = {
  197. # ...
  198. "loggers": {
  199. "my_app": {...},
  200. },
  201. }
  202. You can also define logger namespacing explicitly::
  203. logger = logging.getLogger("project.payment")
  204. and set up logger mappings accordingly.
  205. .. _naming-loggers-hierarchy:
  206. Using logger hierarchies and propagation
  207. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  208. Logger naming is *hierarchical*. ``my_app`` is the parent of ``my_app.views``,
  209. which is the parent of ``my_app.views.private``. Unless specified otherwise,
  210. logger mappings will propagate the records they process to their parents - a
  211. record from a logger in the ``my_app.views.private`` namespace will be handled
  212. by a mapping for both ``my_app`` and ``my_app.views``.
  213. To manage this behavior, set the propagation key on the mappings you define::
  214. LOGGING = {
  215. # ...
  216. "loggers": {
  217. "my_app": {
  218. # ...
  219. },
  220. "my_app.views": {
  221. # ...
  222. },
  223. "my_app.views.private": {
  224. # ...
  225. "propagate": False,
  226. },
  227. },
  228. }
  229. ``propagate`` defaults to ``True``. In this example, the logs from
  230. ``my_app.views.private`` will not be handled by the parent, but logs from
  231. ``my_app.views`` will.
  232. Configure responsive logging
  233. ----------------------------
  234. Logging is most useful when it contains as much information as possible, but
  235. not information that you don't need - and how much you need depends upon what
  236. you're doing. When you're debugging, you need a level of information that would
  237. be excessive and unhelpful if you had to deal with it in production.
  238. You can configure logging to provide you with the level of detail you need,
  239. when you need it. Rather than manually change configuration to achieve this, a
  240. better way is to apply configuration automatically according to the
  241. environment.
  242. For example, you could set an environment variable ``DJANGO_LOG_LEVEL``
  243. appropriately in your development and staging environments, and make use of it
  244. in a logger mapping thus::
  245. "level": os.getenv("DJANGO_LOG_LEVEL", "WARNING")
  246. \- so that unless the environment specifies a lower log level, this
  247. configuration will only forward records of severity ``WARNING`` and above to
  248. its handler.
  249. Other options in the configuration (such as the ``level`` or ``formatter``
  250. option of handlers) can be similarly managed.