PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

Plain Text | 543 lines | 411 code | 132 blank | 0 comment | 0 complexity | a92f803255c65eba4ee6d7b94e0a098f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. =======
  2. Logging
  3. =======
  4. .. versionadded:: 1.3
  5. .. module:: django.utils.log
  6. :synopsis: Logging tools for Django applications
  7. A quick logging primer
  8. ======================
  9. Django uses Python's builtin logging module to perform system logging.
  10. The usage of the logging module is discussed in detail in `Python's
  11. own documentation`_. However, if you've never used Python's logging
  12. framework (or even if you have), here's a quick primer.
  13. .. _Python's own documentation:
  14. The cast of players
  15. -------------------
  16. A Python logging configuration consists of four parts:
  17. * :ref:`topic-logging-parts-loggers`
  18. * :ref:`topic-logging-parts-handlers`
  19. * :ref:`topic-logging-parts-filters`
  20. * :ref:`topic-logging-parts-formatters`
  21. .. _topic-logging-parts-loggers:
  22. Loggers
  23. ~~~~~~~
  24. A logger is the entry point into the logging system. Each logger is
  25. a named bucket to which messages can be written for processing.
  26. A logger is configured to have a *log level*. This log level describes
  27. the severity of the messages that the logger will handle. Python
  28. defines the following log levels:
  29. * ``DEBUG``: Low level system information for debugging purposes
  30. * ``INFO``: General system information
  31. * ``WARNING``: Information describing a minor problem that has
  32. occurred.
  33. * ``ERROR``: Information describing a major problem that has
  34. occurred.
  35. * ``CRITICAL``: Information describing a critical problem that has
  36. occurred.
  37. Each message that is written to the logger is a *Log Record*. Each log
  38. record also has a *log level* indicating the severity of that specific
  39. message. A log record can also contain useful metadata that describes
  40. the event that is being logged. This can include details such as a
  41. stack trace or an error code.
  42. When a message is given to the logger, the log level of the message is
  43. compared to the log level of the logger. If the log level of the
  44. message meets or exceeds the log level of the logger itself, the
  45. message will undergo further processing. If it doesn't, the message
  46. will be ignored.
  47. Once a logger has determined that a message needs to be processed,
  48. it is passed to a *Handler*.
  49. .. _topic-logging-parts-handlers:
  50. Handlers
  51. ~~~~~~~~
  52. The handler is the engine that determines what happens to each message
  53. in a logger. It describes a particular logging behavior, such as
  54. writing a message to the screen, to a file, or to a network socket.
  55. Like loggers, handlers also have a log level. If the log level of a
  56. log record doesn't meet or exceed the level of the handler, the
  57. handler will ignore the message.
  58. A logger can have multiple handlers, and each handler can have a
  59. different log level. In this way, it is possible to provide different
  60. forms of notification depending on the importance of a message. For
  61. example, you could install one handler that forwards ``ERROR`` and
  62. ``CRITICAL`` messages to a paging service, while a second handler
  63. logs all messages (including ``ERROR`` and ``CRITICAL`` messages) to a
  64. file for later analysis.
  65. .. _topic-logging-parts-filters:
  66. Filters
  67. ~~~~~~~
  68. A filter is used to provide additional control over which log records
  69. are passed from logger to handler.
  70. By default, any log message that meets log level requirements will be
  71. handled. However, by installing a filter, you can place additional
  72. criteria on the logging process. For example, you could install a
  73. filter that only allows ``ERROR`` messages from a particular source to
  74. be emitted.
  75. Filters can also be used to modify the logging record prior to being
  76. emitted. For example, you could write a filter that downgrades
  77. ``ERROR`` log records to ``WARNING`` records if a particular set of
  78. criteria are met.
  79. Filters can be installed on loggers or on handlers; multiple filters
  80. can be used in a chain to perform multiple filtering actions.
  81. .. _topic-logging-parts-formatters:
  82. Formatters
  83. ~~~~~~~~~~
  84. Ultimately, a log record needs to be rendered as text. Formatters
  85. describe the exact format of that text. A formatter usually consists
  86. of a Python formatting string; however, you can also write custom
  87. formatters to implement specific formatting behavior.
  88. Using logging
  89. =============
  90. Once you have configured your loggers, handlers, filters and
  91. formatters, you need to place logging calls into your code. Using the
  92. logging framework is very simple. Here's an example::
  93. # import the logging library
  94. import logging
  95. # Get an instance of a logger
  96. logger = logging.getLogger(__name__)
  97. def my_view(request, arg1, arg):
  98. ...
  99. if bad_mojo:
  100. # Log an error message
  101. logger.error('Something went wrong!')
  102. And that's it! Every time the ``bad_mojo`` condition is activated, an
  103. error log record will be written.
  104. Naming loggers
  105. --------------
  106. The call to :meth:`logging.getLogger()` obtains (creating, if
  107. necessary) an instance of a logger. The logger instance is identified
  108. by a name. This name is used to identify the logger for configuration
  109. purposes.
  110. By convention, the logger name is usually ``__name__``, the name of
  111. the python module that contains the logger. This allows you to filter
  112. and handle logging calls on a per-module basis. However, if you have
  113. some other way of organizing your logging messages, you can provide
  114. any dot-separated name to identify your logger::
  115. # Get an instance of a specific named logger
  116. logger = logging.getLogger('project.interesting.stuff')
  117. The dotted paths of logger names define a hierarchy. The
  118. ``project.interesting`` logger is considered to be a parent of the
  119. ``project.interesting.stuff`` logger; the ``project`` logger
  120. is a parent of the ``project.interesting`` logger.
  121. Why is the hierarchy important? Well, because loggers can be set to
  122. *propagate* their logging calls to their parents. In this way, you can
  123. define a single set of handlers at the root of a logger tree, and
  124. capture all logging calls in the subtree of loggers. A logging handler
  125. defined in the ``project`` namespace will catch all logging messages
  126. issued on the ``project.interesting`` and
  127. ``project.interesting.stuff`` loggers.
  128. This propagation can be controlled on a per-logger basis. If
  129. you don't want a particular logger to propagate to it's parents, you
  130. can turn off this behavior.
  131. Making logging calls
  132. --------------------
  133. The logger instance contains an entry method for each of the default
  134. log levels:
  135. * ``logger.critical()``
  136. * ``logger.error()``
  137. * ``logger.warning()``
  138. * ````
  139. * ``logger.debug()``
  140. There are two other logging calls available:
  141. * ``logger.log()``: Manually emits a logging message with a
  142. specific log level.
  143. * ``logger.exception()``: Creates an ``ERROR`` level logging
  144. message wrapping the current exception stack frame.
  145. Configuring logging
  146. ===================
  147. Of course, it isn't enough to just put logging calls into your code.
  148. You also need to configure the loggers, handlers, filters and
  149. formatters to ensure that logging output is output in a useful way.
  150. Python's logging library provides several techniques to configure
  151. logging, ranging from a programmatic interface to configuration files.
  152. By default, Django uses the `dictConfig format`_.
  153. .. note::
  154. ``logging.dictConfig`` is a builtin library in Python 2.7. In
  155. order to make this library available for users of earlier Python
  156. versions, Django includes a copy as part of ``django.utils.log``.
  157. If you have Python 2.7, the system native library will be used; if
  158. you have Python 2.6 or earlier, Django's copy will be used.
  159. In order to configure logging, you use :setting:`LOGGING` to define a
  160. dictionary of logging settings. These settings describes the loggers,
  161. handlers, filters and formatters that you want in your logging setup,
  162. and the log levels and other properties that you want those components
  163. to have.
  164. Logging is configured immediately after settings have been loaded.
  165. Since the loading of settings is one of the first things that Django
  166. does, you can be certain that loggers are always ready for use in your
  167. project code.
  168. .. _dictConfig format:
  169. .. _a third-party library:
  170. An example
  171. ----------
  172. The full documentation for `dictConfig format`_ is the best source of
  173. information about logging configuration dictionaries. However, to give
  174. you a taste of what is possible, here is an example of a fairly
  175. complex logging setup, configured using :meth:`logging.dictConfig`::
  176. LOGGING = {
  177. 'version': 1,
  178. 'disable_existing_loggers': True,
  179. 'formatters': {
  180. 'verbose': {
  181. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  182. },
  183. 'simple': {
  184. 'format': '%(levelname)s %(message)s'
  185. },
  186. },
  187. 'filters': {
  188. 'special': {
  189. '()': 'project.logging.SpecialFilter',
  190. 'foo': 'bar',
  191. }
  192. },
  193. 'handlers': {
  194. 'null': {
  195. 'level':'DEBUG',
  196. 'class':'django.utils.log.NullHandler',
  197. },
  198. 'console':{
  199. 'level':'DEBUG',
  200. 'class':'logging.StreamHandler',
  201. 'formatter': 'simple'
  202. },
  203. 'mail_admins': {
  204. 'level': 'ERROR',
  205. 'class': 'django.utils.log.AdminEmailHandler',
  206. 'filters': ['special']
  207. }
  208. },
  209. 'loggers': {
  210. 'django': {
  211. 'handlers':['null'],
  212. 'propagate': True,
  213. 'level':'INFO',
  214. },
  215. 'django.request': {
  216. 'handlers': ['mail_admins'],
  217. 'level': 'ERROR',
  218. 'propagate': False,
  219. },
  220. 'myproject.custom': {
  221. 'handlers': ['console', 'mail_admins'],
  222. 'level': 'INFO',
  223. 'filters': ['special']
  224. }
  225. }
  226. }
  227. This logging configuration does the following things:
  228. * Identifies the configuration as being in 'dictConfig version 1'
  229. format. At present, this is the only dictConfig format version.
  230. * Disables all existing logging configurations.
  231. * Defines two formatters:
  232. * ``simple``, that just outputs the log level name (e.g.,
  233. ``DEBUG``) and the log message.
  234. The `format` string is a normal Python formatting string
  235. describing the details that are to be output on each logging
  236. line. The full list of detail that can be output can be
  237. found in the `formatter documentation`_.
  238. * ``verbose``, that outputs the log level name, the log
  239. message, plus the time, process, thread and module that
  240. generate the log message.
  241. * Defines one filter -- :class:`project.logging.SpecialFilter`,
  242. using the alias ``special``. If this filter required additional
  243. arguments at time of construction, they can be provided as
  244. additional keys in the filter configuration dictionary. In this
  245. case, the argument ``foo`` will be given a value of ``bar`` when
  246. instantiating the :class:`SpecialFilter`.
  247. * Defines three handlers:
  248. * ``null``, a NullHandler, which will pass any ``DEBUG`` or
  249. higher message to ``/dev/null``.
  250. * ``console``, a StreamHandler, which will print any ``DEBUG``
  251. message to stdout. This handler uses the `simple` output
  252. format.
  253. * ``mail_admins``, an AdminEmailHandler, which will e-mail any
  254. ``ERROR`` level message to the site admins. This handler uses
  255. the ``special`` filter.
  256. * Configures three loggers:
  257. * ``django``, which passes all messages at ``INFO`` or higher
  258. to the ``null`` handler.
  259. * ``django.request``, which passes all ``ERROR`` messages to
  260. the ``mail_admins`` handler. In addition, this logger is
  261. marked to *not* propagate messages. This means that log
  262. messages written to ``django.request`` will not be handled
  263. by the ``django`` logger.
  264. * ``myproject.custom``, which passes all messages at ``INFO``
  265. or higher that also pass the ``special`` filter to two
  266. handlers -- the ``console``, and ``mail_admins``. This
  267. means that all ``INFO`` level messages (or higher) will be
  268. printed to the console; ``ERROR`` and ``CRITICAL``
  269. messages will also be output via e-mail.
  270. .. admonition:: Custom handlers and circular imports
  271. If your ```` specifies a custom handler class and the file
  272. defining that class also imports ```` a circular import will
  273. occur.
  274. For example, if ```` contains the following config for
  275. :setting:`LOGGING`::
  276. LOGGING = {
  277. 'version': 1,
  278. 'handlers': {
  279. 'custom_handler': {
  280. 'level': 'INFO',
  281. 'class': 'myproject.logconfig.MyHandler',
  282. }
  283. }
  284. }
  285. and ``myproject/`` has the following line before the
  286. ``MyHandler`` definition::
  287. from django.conf import settings
  288. then the ``dictconfig`` module will raise an exception like the following::
  289. ValueError: Unable to configure handler 'custom_handler':
  290. Unable to configure handler 'custom_handler':
  291. 'module' object has no attribute 'logconfig'
  292. .. _formatter documentation:
  293. Custom logging configuration
  294. ----------------------------
  295. If you don't want to use Python's dictConfig format to configure your
  296. logger, you can specify your own configuration scheme.
  297. The :setting:`LOGGING_CONFIG` setting defines the callable that will
  298. be used to configure Django's loggers. By default, it points at
  299. Python's :meth:`logging.dictConfig()` method. However, if you want to
  300. use a different configuration process, you can use any other callable
  301. that takes a single argument. The contents of :setting:`LOGGING` will
  302. be provided as the value of that argument when logging is configured.
  303. Disabling logging configuration
  304. -------------------------------
  305. If you don't want to configure logging at all (or you want to manually
  306. configure logging using your own approach), you can set
  307. :setting:`LOGGING_CONFIG` to ``None``. This will disable the
  308. configuration process.
  309. .. note::
  310. Setting :setting:`LOGGING_CONFIG` to ``None`` only means that the
  311. configuration process is disabled, not logging itself. If you
  312. disable the configuration process, Django will still make logging
  313. calls, falling back to whatever default logging behavior is
  314. defined.
  315. Django's logging extensions
  316. ===========================
  317. Django provides a number of utilities to handle the unique
  318. requirements of logging in Web server environment.
  319. Loggers
  320. -------
  321. Django provides three built-in loggers.
  322. ``django``
  323. ~~~~~~~~~~
  324. ``django`` is the catch-all logger. No messages are posted directly to
  325. this logger.
  326. ``django.request``
  327. ~~~~~~~~~~~~~~~~~~
  328. Log messages related to the handling of requests. 5XX responses are
  329. raised as ``ERROR`` messages; 4XX responses are raised as ``WARNING``
  330. messages.
  331. Messages to this logger have the following extra context:
  332. * ``status_code``: The HTTP response code associated with the
  333. request.
  334. * ``request``: The request object that generated the logging
  335. message.
  336. .. note::
  337. Due to a limitation in the logging library, this extra
  338. context is not available if you are using Python 2.4.
  339. ``django.db.backends``
  340. ~~~~~~~~~~~~~~~~~~~~~~
  341. Messages relating to the interaction of code with the database.
  342. For example, every SQL statement executed by a request is logged
  343. at the ``DEBUG`` level to this logger.
  344. Messages to this logger have the following extra context:
  345. * ``duration``: The time taken to execute the SQL statement.
  346. * ``sql``: The SQL statement that was executed.
  347. * ``params``: The parameters that were used in the SQL call.
  348. For performance reasons, SQL logging is only enabled when
  349. ``settings.DEBUG`` is set to ``True``, regardless of the logging
  350. level or handlers that are installed.
  351. .. note::
  352. Due to a limitation in the logging library, this extra
  353. context is not available if you are using Python 2.4.
  354. Handlers
  355. --------
  356. Django provides one log handler in addition to those provided by the
  357. Python logging module.
  358. .. class:: AdminEmailHandler([include_html=False])
  359. This handler sends an e-mail to the site admins for each log
  360. message it receives.
  361. If the log record contains a ``request`` attribute, the full details
  362. of the request will be included in the e-mail.
  363. If the log record contains stack trace information, that stack
  364. trace will be included in the e-mail.
  365. The ``include_html`` argument of ``AdminEmailHandler`` is used to
  366. control whether the traceback e-mail includes an HTML attachment
  367. containing the full content of the debug Web page that would have been
  368. produced if :setting:`DEBUG` were ``True``. To set this value in your
  369. configuration, include it in the handler definition for
  370. ``django.utils.log.AdminEmailHandler``, like this::
  371. 'handlers': {
  372. 'mail_admins': {
  373. 'level': 'ERROR',
  374. 'class': 'django.utils.log.AdminEmailHandler',
  375. 'include_html': True,
  376. }
  377. },
  378. Note that this HTML version of the e-mail contains a full traceback,
  379. with names and values of local variables at each level of the stack, plus
  380. the values of your Django settings. This information is potentially very
  381. sensitive, and you may not want to send it over e-mail. Consider using
  382. something such as `django-sentry`_ to get the best of both worlds -- the
  383. rich information of full tracebacks plus the security of *not* sending the
  384. information over e-mail.
  385. .. _django-sentry:
  386. Filters
  387. -------
  388. Django provides one log filter in addition to those provided by the
  389. Python logging module.
  390. .. class:: CallbackFilter(callback)
  391. .. versionadded:: 1.4
  392. This filter accepts a callback function (which should accept a single
  393. argument, the record to be logged), and calls it for each record that passes
  394. through the filter. Handling of that record will not proceed if the callback
  395. returns False.
  396. This filter is used as follows in the default :setting:`LOGGING`
  397. configuration to ensure that the :class:`AdminEmailHandler` only sends error
  398. emails to admins when :setting:`DEBUG` is `False`::
  399. 'filters': {
  400. 'require_debug_false': {
  401. '()': 'django.utils.log.CallbackFilter',
  402. 'callback': lambda r: not DEBUG
  403. }
  404. },
  405. 'handlers': {
  406. 'mail_admins': {
  407. 'level': 'ERROR',
  408. 'filters': ['require_debug_false'],
  409. 'class': 'django.utils.log.AdminEmailHandler'
  410. }
  411. },