2010-10-04 17:12:39 +02:00
|
|
|
=======
|
|
|
|
Logging
|
|
|
|
=======
|
|
|
|
|
|
|
|
.. module:: django.utils.log
|
|
|
|
:synopsis: Logging tools for Django applications
|
|
|
|
|
|
|
|
A quick logging primer
|
|
|
|
======================
|
|
|
|
|
2011-09-04 23:17:30 +02:00
|
|
|
Django uses Python's builtin :mod:`logging` module to perform system logging.
|
|
|
|
The usage of this module is discussed in detail in Python's own documentation.
|
|
|
|
However, if you've never used Python's logging framework (or even if you have),
|
|
|
|
here's a quick primer.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
The cast of players
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
A Python logging configuration consists of four parts:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* :ref:`topic-logging-parts-loggers`
|
|
|
|
* :ref:`topic-logging-parts-handlers`
|
|
|
|
* :ref:`topic-logging-parts-filters`
|
|
|
|
* :ref:`topic-logging-parts-formatters`
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
.. _topic-logging-parts-loggers:
|
|
|
|
|
|
|
|
Loggers
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
A logger is the entry point into the logging system. Each logger is
|
|
|
|
a named bucket to which messages can be written for processing.
|
|
|
|
|
2010-10-05 02:11:41 +02:00
|
|
|
A logger is configured to have a *log level*. This log level describes
|
2010-10-04 17:12:39 +02:00
|
|
|
the severity of the messages that the logger will handle. Python
|
|
|
|
defines the following log levels:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``DEBUG``: Low level system information for debugging purposes
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``INFO``: General system information
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``WARNING``: Information describing a minor problem that has
|
|
|
|
occurred.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``ERROR``: Information describing a major problem that has
|
|
|
|
occurred.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``CRITICAL``: Information describing a critical problem that has
|
|
|
|
occurred.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Each message that is written to the logger is a *Log Record*. Each log
|
|
|
|
record also has a *log level* indicating the severity of that specific
|
|
|
|
message. A log record can also contain useful metadata that describes
|
|
|
|
the event that is being logged. This can include details such as a
|
|
|
|
stack trace or an error code.
|
|
|
|
|
|
|
|
When a message is given to the logger, the log level of the message is
|
2010-10-05 02:11:41 +02:00
|
|
|
compared to the log level of the logger. If the log level of the
|
2010-10-04 17:12:39 +02:00
|
|
|
message meets or exceeds the log level of the logger itself, the
|
|
|
|
message will undergo further processing. If it doesn't, the message
|
|
|
|
will be ignored.
|
|
|
|
|
|
|
|
Once a logger has determined that a message needs to be processed,
|
|
|
|
it is passed to a *Handler*.
|
|
|
|
|
|
|
|
.. _topic-logging-parts-handlers:
|
|
|
|
|
|
|
|
Handlers
|
|
|
|
~~~~~~~~
|
|
|
|
|
|
|
|
The handler is the engine that determines what happens to each message
|
|
|
|
in a logger. It describes a particular logging behavior, such as
|
|
|
|
writing a message to the screen, to a file, or to a network socket.
|
|
|
|
|
|
|
|
Like loggers, handlers also have a log level. If the log level of a
|
|
|
|
log record doesn't meet or exceed the level of the handler, the
|
|
|
|
handler will ignore the message.
|
|
|
|
|
|
|
|
A logger can have multiple handlers, and each handler can have a
|
|
|
|
different log level. In this way, it is possible to provide different
|
|
|
|
forms of notification depending on the importance of a message. For
|
|
|
|
example, you could install one handler that forwards ``ERROR`` and
|
2010-10-05 02:11:41 +02:00
|
|
|
``CRITICAL`` messages to a paging service, while a second handler
|
2010-10-04 17:12:39 +02:00
|
|
|
logs all messages (including ``ERROR`` and ``CRITICAL`` messages) to a
|
|
|
|
file for later analysis.
|
|
|
|
|
|
|
|
.. _topic-logging-parts-filters:
|
|
|
|
|
|
|
|
Filters
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
A filter is used to provide additional control over which log records
|
|
|
|
are passed from logger to handler.
|
|
|
|
|
|
|
|
By default, any log message that meets log level requirements will be
|
|
|
|
handled. However, by installing a filter, you can place additional
|
|
|
|
criteria on the logging process. For example, you could install a
|
|
|
|
filter that only allows ``ERROR`` messages from a particular source to
|
|
|
|
be emitted.
|
|
|
|
|
|
|
|
Filters can also be used to modify the logging record prior to being
|
|
|
|
emitted. For example, you could write a filter that downgrades
|
|
|
|
``ERROR`` log records to ``WARNING`` records if a particular set of
|
|
|
|
criteria are met.
|
|
|
|
|
|
|
|
Filters can be installed on loggers or on handlers; multiple filters
|
|
|
|
can be used in a chain to perform multiple filtering actions.
|
|
|
|
|
|
|
|
.. _topic-logging-parts-formatters:
|
|
|
|
|
|
|
|
Formatters
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Ultimately, a log record needs to be rendered as text. Formatters
|
|
|
|
describe the exact format of that text. A formatter usually consists
|
2014-09-11 21:37:16 +02:00
|
|
|
of a Python formatting string containing
|
|
|
|
:ref:`LogRecord attributes <python:logrecord-attributes>`; however,
|
|
|
|
you can also write custom formatters to implement specific formatting behavior.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Using logging
|
|
|
|
=============
|
|
|
|
|
|
|
|
Once you have configured your loggers, handlers, filters and
|
|
|
|
formatters, you need to place logging calls into your code. Using the
|
2019-06-17 16:54:55 +02:00
|
|
|
logging framework works like this::
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
# import the logging library
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# Get an instance of a logger
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def my_view(request, arg1, arg):
|
|
|
|
...
|
|
|
|
if bad_mojo:
|
|
|
|
# Log an error message
|
|
|
|
logger.error('Something went wrong!')
|
|
|
|
|
|
|
|
And that's it! Every time the ``bad_mojo`` condition is activated, an
|
|
|
|
error log record will be written.
|
|
|
|
|
|
|
|
Naming loggers
|
2010-10-05 02:11:41 +02:00
|
|
|
--------------
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2013-01-01 14:12:42 +01:00
|
|
|
The call to :func:`logging.getLogger()` obtains (creating, if
|
2010-10-04 17:12:39 +02:00
|
|
|
necessary) an instance of a logger. The logger instance is identified
|
|
|
|
by a name. This name is used to identify the logger for configuration
|
|
|
|
purposes.
|
|
|
|
|
|
|
|
By convention, the logger name is usually ``__name__``, the name of
|
2018-10-09 15:26:07 +02:00
|
|
|
the Python module that contains the logger. This allows you to filter
|
2010-10-04 17:12:39 +02:00
|
|
|
and handle logging calls on a per-module basis. However, if you have
|
|
|
|
some other way of organizing your logging messages, you can provide
|
|
|
|
any dot-separated name to identify your logger::
|
|
|
|
|
2011-05-13 06:33:42 +02:00
|
|
|
# Get an instance of a specific named logger
|
2010-10-04 17:12:39 +02:00
|
|
|
logger = logging.getLogger('project.interesting.stuff')
|
|
|
|
|
|
|
|
The dotted paths of logger names define a hierarchy. The
|
|
|
|
``project.interesting`` logger is considered to be a parent of the
|
|
|
|
``project.interesting.stuff`` logger; the ``project`` logger
|
|
|
|
is a parent of the ``project.interesting`` logger.
|
|
|
|
|
|
|
|
Why is the hierarchy important? Well, because loggers can be set to
|
|
|
|
*propagate* their logging calls to their parents. In this way, you can
|
|
|
|
define a single set of handlers at the root of a logger tree, and
|
|
|
|
capture all logging calls in the subtree of loggers. A logging handler
|
|
|
|
defined in the ``project`` namespace will catch all logging messages
|
|
|
|
issued on the ``project.interesting`` and
|
|
|
|
``project.interesting.stuff`` loggers.
|
|
|
|
|
|
|
|
This propagation can be controlled on a per-logger basis. If
|
2013-04-29 15:30:51 +02:00
|
|
|
you don't want a particular logger to propagate to its parents, you
|
2010-10-04 17:12:39 +02:00
|
|
|
can turn off this behavior.
|
|
|
|
|
|
|
|
Making logging calls
|
2010-10-05 02:11:41 +02:00
|
|
|
--------------------
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
The logger instance contains an entry method for each of the default
|
|
|
|
log levels:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``logger.debug()``
|
2013-11-15 14:38:23 +01:00
|
|
|
* ``logger.info()``
|
|
|
|
* ``logger.warning()``
|
|
|
|
* ``logger.error()``
|
|
|
|
* ``logger.critical()``
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
There are two other logging calls available:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``logger.log()``: Manually emits a logging message with a
|
|
|
|
specific log level.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``logger.exception()``: Creates an ``ERROR`` level logging
|
|
|
|
message wrapping the current exception stack frame.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2012-09-24 22:30:38 +02:00
|
|
|
.. _configuring-logging:
|
|
|
|
|
2010-10-04 17:12:39 +02:00
|
|
|
Configuring logging
|
|
|
|
===================
|
|
|
|
|
|
|
|
Of course, it isn't enough to just put logging calls into your code.
|
|
|
|
You also need to configure the loggers, handlers, filters and
|
|
|
|
formatters to ensure that logging output is output in a useful way.
|
|
|
|
|
|
|
|
Python's logging library provides several techniques to configure
|
2010-10-05 02:11:41 +02:00
|
|
|
logging, ranging from a programmatic interface to configuration files.
|
2016-05-09 00:07:43 +02:00
|
|
|
By default, Django uses the :ref:`dictConfig format
|
|
|
|
<logging-config-dictschema>`.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
In order to configure logging, you use :setting:`LOGGING` to define a
|
|
|
|
dictionary of logging settings. These settings describes the loggers,
|
|
|
|
handlers, filters and formatters that you want in your logging setup,
|
|
|
|
and the log levels and other properties that you want those components
|
|
|
|
to have.
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
By default, the :setting:`LOGGING` setting is merged with :ref:`Django's
|
|
|
|
default logging configuration <default-logging-configuration>` using the
|
|
|
|
following scheme.
|
2013-09-16 22:49:46 +02:00
|
|
|
|
|
|
|
If the ``disable_existing_loggers`` key in the :setting:`LOGGING` dictConfig is
|
2019-06-09 21:28:30 +02:00
|
|
|
set to ``True`` (which is the ``dictConfig`` default if the key is missing)
|
|
|
|
then all loggers from the default configuration will be disabled. Disabled
|
|
|
|
loggers are not the same as removed; the logger will still exist, but will
|
|
|
|
silently discard anything logged to it, not even propagating entries to a
|
|
|
|
parent logger. Thus you should be very careful using
|
|
|
|
``'disable_existing_loggers': True``; it's probably not what you want. Instead,
|
|
|
|
you can set ``disable_existing_loggers`` to ``False`` and redefine some or all
|
|
|
|
of the default loggers; or you can set :setting:`LOGGING_CONFIG` to ``None``
|
|
|
|
and :ref:`handle logging config yourself <disabling-logging-configuration>`.
|
2012-09-24 22:30:38 +02:00
|
|
|
|
2013-12-31 12:28:01 +01:00
|
|
|
Logging is configured as part of the general Django ``setup()`` function.
|
|
|
|
Therefore, you can be certain that loggers are always ready for use in your
|
|
|
|
project code.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2013-12-31 16:20:44 +01:00
|
|
|
Examples
|
|
|
|
--------
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2016-05-09 00:07:43 +02:00
|
|
|
The full documentation for :ref:`dictConfig format <logging-config-dictschema>`
|
|
|
|
is the best source of information about logging configuration dictionaries.
|
|
|
|
However, to give you a taste of what is possible, here are several examples.
|
2013-12-31 16:20:44 +01:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
First, here's a configuration which writes all logging from the
|
2015-12-24 15:51:27 +01:00
|
|
|
:ref:`django-logger` logger to a local file::
|
2013-12-31 16:20:44 +01:00
|
|
|
|
2019-05-16 16:51:36 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: settings.py
|
|
|
|
|
2013-12-31 16:20:44 +01:00
|
|
|
LOGGING = {
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'handlers': {
|
|
|
|
'file': {
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'class': 'logging.FileHandler',
|
|
|
|
'filename': '/path/to/django/debug.log',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'loggers': {
|
2015-12-24 15:51:27 +01:00
|
|
|
'django': {
|
2013-12-31 16:20:44 +01:00
|
|
|
'handlers': ['file'],
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'propagate': True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
If you use this example, be sure to change the ``'filename'`` path to a
|
|
|
|
location that's writable by the user that's running the Django application.
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
Second, here's an example of how to make the logging system print Django's
|
2015-03-23 23:17:43 +01:00
|
|
|
logging to the console. It may be useful during local development.
|
2015-03-21 18:59:23 +01:00
|
|
|
|
|
|
|
By default, this config only sends messages of level ``INFO`` or higher to the
|
2015-03-23 23:17:43 +01:00
|
|
|
console (same as Django's default logging config, except that the default only
|
|
|
|
displays log records when ``DEBUG=True``). Django does not log many such
|
|
|
|
messages. With this config, however, you can also set the environment variable
|
2015-03-21 18:59:23 +01:00
|
|
|
``DJANGO_LOG_LEVEL=DEBUG`` to see all of Django's debug logging which is very
|
|
|
|
verbose as it includes all database queries::
|
|
|
|
|
2019-05-16 16:51:36 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: settings.py
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
import os
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
LOGGING = {
|
|
|
|
'version': 1,
|
2015-03-21 18:59:23 +01:00
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'handlers': {
|
|
|
|
'console': {
|
|
|
|
'class': 'logging.StreamHandler',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django': {
|
|
|
|
'handlers': ['console'],
|
|
|
|
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Finally, here's an example of a fairly complex logging setup::
|
|
|
|
|
2019-05-16 16:51:36 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: settings.py
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
LOGGING = {
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
2010-10-04 17:12:39 +02:00
|
|
|
'formatters': {
|
2010-10-05 03:44:12 +02:00
|
|
|
'verbose': {
|
2017-11-19 01:15:08 +01:00
|
|
|
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
|
|
|
|
'style': '{',
|
2010-10-04 17:12:39 +02:00
|
|
|
},
|
|
|
|
'simple': {
|
2017-11-19 01:15:08 +01:00
|
|
|
'format': '{levelname} {message}',
|
|
|
|
'style': '{',
|
2010-10-04 17:12:39 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
'filters': {
|
|
|
|
'special': {
|
|
|
|
'()': 'project.logging.SpecialFilter',
|
|
|
|
'foo': 'bar',
|
2015-11-07 17:22:30 +01:00
|
|
|
},
|
|
|
|
'require_debug_true': {
|
|
|
|
'()': 'django.utils.log.RequireDebugTrue',
|
|
|
|
},
|
2010-10-04 17:12:39 +02:00
|
|
|
},
|
|
|
|
'handlers': {
|
2014-10-15 23:48:35 +02:00
|
|
|
'console': {
|
2015-11-07 17:22:30 +01:00
|
|
|
'level': 'INFO',
|
|
|
|
'filters': ['require_debug_true'],
|
2012-09-15 22:45:13 +02:00
|
|
|
'class': 'logging.StreamHandler',
|
2010-10-04 17:12:39 +02:00
|
|
|
'formatter': 'simple'
|
|
|
|
},
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
2010-10-06 16:57:31 +02:00
|
|
|
'class': 'django.utils.log.AdminEmailHandler',
|
2010-10-04 17:12:39 +02:00
|
|
|
'filters': ['special']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django': {
|
2015-11-07 17:22:30 +01:00
|
|
|
'handlers': ['console'],
|
2010-10-04 17:12:39 +02:00
|
|
|
'propagate': True,
|
|
|
|
},
|
|
|
|
'django.request': {
|
|
|
|
'handlers': ['mail_admins'],
|
|
|
|
'level': 'ERROR',
|
|
|
|
'propagate': False,
|
|
|
|
},
|
|
|
|
'myproject.custom': {
|
2010-10-06 16:57:31 +02:00
|
|
|
'handlers': ['console', 'mail_admins'],
|
2010-10-04 17:12:39 +02:00
|
|
|
'level': 'INFO',
|
|
|
|
'filters': ['special']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
This logging configuration does the following things:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* Identifies the configuration as being in 'dictConfig version 1'
|
|
|
|
format. At present, this is the only dictConfig format version.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* Defines two formatters:
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
* ``simple``, that outputs the log level name (e.g., ``DEBUG``) and the log
|
|
|
|
message.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2013-03-22 10:50:45 +01:00
|
|
|
The ``format`` string is a normal Python formatting string
|
2011-10-14 02:12:01 +02:00
|
|
|
describing the details that are to be output on each logging
|
|
|
|
line. The full list of detail that can be output can be
|
2016-05-09 00:07:43 +02:00
|
|
|
found in :ref:`formatter-objects`.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``verbose``, that outputs the log level name, the log
|
|
|
|
message, plus the time, process, thread and module that
|
|
|
|
generate the log message.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-11-07 17:22:30 +01:00
|
|
|
* Defines two filters:
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-11-07 17:22:30 +01:00
|
|
|
* ``project.logging.SpecialFilter``, using the alias ``special``. If this
|
|
|
|
filter required additional arguments, they can be provided as additional
|
|
|
|
keys in the filter configuration dictionary. In this case, the argument
|
|
|
|
``foo`` will be given a value of ``bar`` when instantiating
|
|
|
|
``SpecialFilter``.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-11-07 17:22:30 +01:00
|
|
|
* ``django.utils.log.RequireDebugTrue``, which passes on records when
|
|
|
|
:setting:`DEBUG` is ``True``.
|
|
|
|
|
|
|
|
* Defines two handlers:
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2017-11-21 11:21:31 +01:00
|
|
|
* ``console``, a :class:`~logging.StreamHandler`, which prints any ``INFO``
|
|
|
|
(or higher) message to ``sys.stderr``. This handler uses the ``simple``
|
|
|
|
output format.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2017-11-21 11:21:31 +01:00
|
|
|
* ``mail_admins``, an :class:`AdminEmailHandler`, which emails any ``ERROR``
|
|
|
|
(or higher) message to the site :setting:`ADMINS`. This handler uses the
|
|
|
|
``special`` filter.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* Configures three loggers:
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-11-07 17:22:30 +01:00
|
|
|
* ``django``, which passes all messages to the ``console`` handler.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``django.request``, which passes all ``ERROR`` messages to
|
|
|
|
the ``mail_admins`` handler. In addition, this logger is
|
|
|
|
marked to *not* propagate messages. This means that log
|
|
|
|
messages written to ``django.request`` will not be handled
|
|
|
|
by the ``django`` logger.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``myproject.custom``, which passes all messages at ``INFO``
|
|
|
|
or higher that also pass the ``special`` filter to two
|
|
|
|
handlers -- the ``console``, and ``mail_admins``. This
|
|
|
|
means that all ``INFO`` level messages (or higher) will be
|
|
|
|
printed to the console; ``ERROR`` and ``CRITICAL``
|
|
|
|
messages will also be output via email.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Custom logging configuration
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
If you don't want to use Python's dictConfig format to configure your
|
|
|
|
logger, you can specify your own configuration scheme.
|
|
|
|
|
|
|
|
The :setting:`LOGGING_CONFIG` setting defines the callable that will
|
|
|
|
be used to configure Django's loggers. By default, it points at
|
2013-01-01 14:12:42 +01:00
|
|
|
Python's :func:`logging.config.dictConfig()` function. However, if you want to
|
2010-10-04 17:12:39 +02:00
|
|
|
use a different configuration process, you can use any other callable
|
|
|
|
that takes a single argument. The contents of :setting:`LOGGING` will
|
|
|
|
be provided as the value of that argument when logging is configured.
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
.. _disabling-logging-configuration:
|
|
|
|
|
2010-10-04 17:12:39 +02:00
|
|
|
Disabling logging configuration
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
If you don't want to configure logging at all (or you want to manually
|
|
|
|
configure logging using your own approach), you can set
|
|
|
|
:setting:`LOGGING_CONFIG` to ``None``. This will disable the
|
2015-03-21 18:59:23 +01:00
|
|
|
configuration process for :ref:`Django's default logging
|
|
|
|
<default-logging-configuration>`. Here's an example that disables Django's
|
|
|
|
logging configuration and then manually configures logging:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: settings.py
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
LOGGING_CONFIG = None
|
|
|
|
|
|
|
|
import logging.config
|
|
|
|
logging.config.dictConfig(...)
|
|
|
|
|
|
|
|
Setting :setting:`LOGGING_CONFIG` to ``None`` only means that the automatic
|
|
|
|
configuration process is disabled, not logging itself. If you disable the
|
|
|
|
configuration process, Django will still make logging calls, falling back to
|
|
|
|
whatever default logging behavior is defined.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Django's logging extensions
|
|
|
|
===========================
|
|
|
|
|
|
|
|
Django provides a number of utilities to handle the unique
|
2010-10-09 10:12:50 +02:00
|
|
|
requirements of logging in Web server environment.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Loggers
|
|
|
|
-------
|
|
|
|
|
2014-08-15 14:18:55 +02:00
|
|
|
Django provides several built-in loggers.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-12-24 15:51:27 +01:00
|
|
|
.. _django-logger:
|
|
|
|
|
2010-10-04 17:12:39 +02:00
|
|
|
``django``
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2017-08-22 22:24:18 +02:00
|
|
|
The catch-all logger for messages in the ``django`` hierarchy. No messages are
|
|
|
|
posted using this name but instead using one of the loggers below.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2013-12-31 16:20:44 +01:00
|
|
|
.. _django-request-logger:
|
|
|
|
|
2010-12-19 20:20:25 +01:00
|
|
|
``django.request``
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Log messages related to the handling of requests. 5XX responses are
|
|
|
|
raised as ``ERROR`` messages; 4XX responses are raised as ``WARNING``
|
2017-07-13 06:09:18 +02:00
|
|
|
messages. Requests that are logged to the ``django.security`` logger aren't
|
|
|
|
logged to ``django.request``.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Messages to this logger have the following extra context:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``status_code``: The HTTP response code associated with the
|
|
|
|
request.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``request``: The request object that generated the logging
|
|
|
|
message.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2015-11-06 17:19:41 +01:00
|
|
|
.. _django-server-logger:
|
|
|
|
|
|
|
|
``django.server``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Log messages related to the handling of requests received by the server invoked
|
|
|
|
by the :djadmin:`runserver` command. HTTP 5XX responses are logged as ``ERROR``
|
|
|
|
messages, 4XX responses are logged as ``WARNING`` messages, and everything else
|
|
|
|
is logged as ``INFO``.
|
|
|
|
|
|
|
|
Messages to this logger have the following extra context:
|
|
|
|
|
|
|
|
* ``status_code``: The HTTP response code associated with the request.
|
|
|
|
|
|
|
|
* ``request``: The request object that generated the logging message.
|
|
|
|
|
2014-04-14 23:18:03 +02:00
|
|
|
.. _django-template-logger:
|
|
|
|
|
|
|
|
``django.template``
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-23 17:47:22 +02:00
|
|
|
Log messages related to the rendering of templates.
|
|
|
|
|
|
|
|
* Missing context variables are logged as ``DEBUG`` messages.
|
|
|
|
|
2015-01-10 23:52:59 +01:00
|
|
|
.. _django-db-logger:
|
|
|
|
|
2010-10-04 17:12:39 +02:00
|
|
|
``django.db.backends``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2013-12-31 19:02:01 +01:00
|
|
|
Messages relating to the interaction of code with the database. For example,
|
|
|
|
every application-level SQL statement executed by a request is logged at the
|
|
|
|
``DEBUG`` level to this logger.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
Messages to this logger have the following extra context:
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* ``duration``: The time taken to execute the SQL statement.
|
|
|
|
* ``sql``: The SQL statement that was executed.
|
|
|
|
* ``params``: The parameters that were used in the SQL call.
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2011-03-23 00:20:52 +01:00
|
|
|
For performance reasons, SQL logging is only enabled when
|
2011-03-23 00:18:09 +01:00
|
|
|
``settings.DEBUG`` is set to ``True``, regardless of the logging
|
|
|
|
level or handlers that are installed.
|
|
|
|
|
2013-12-31 19:02:01 +01:00
|
|
|
This logging does not include framework-level initialization (e.g.
|
|
|
|
``SET TIMEZONE``) or transaction management queries (e.g. ``BEGIN``,
|
|
|
|
``COMMIT``, and ``ROLLBACK``). Turn on query logging in your database if you
|
2014-04-28 16:05:28 +02:00
|
|
|
wish to view all database queries.
|
2013-12-31 19:02:01 +01:00
|
|
|
|
2016-06-03 02:24:48 +02:00
|
|
|
.. _django-security-logger:
|
|
|
|
|
2013-05-16 01:14:28 +02:00
|
|
|
``django.security.*``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The security loggers will receive messages on any occurrence of
|
2016-06-03 02:24:48 +02:00
|
|
|
:exc:`~django.core.exceptions.SuspiciousOperation` and other security-related
|
|
|
|
errors. There is a sub-logger for each subtype of security error, including all
|
|
|
|
``SuspiciousOperation``\s. The level of the log event depends on where the
|
|
|
|
exception is handled. Most occurrences are logged as a warning, while
|
2013-05-16 01:14:28 +02:00
|
|
|
any ``SuspiciousOperation`` that reaches the WSGI handler will be logged as an
|
|
|
|
error. For example, when an HTTP ``Host`` header is included in a request from
|
|
|
|
a client that does not match :setting:`ALLOWED_HOSTS`, Django will return a 400
|
|
|
|
response, and an error message will be logged to the
|
|
|
|
``django.security.DisallowedHost`` logger.
|
|
|
|
|
2016-06-03 02:24:48 +02:00
|
|
|
These log events will reach the ``django`` logger by default, which mails error
|
2015-03-23 23:17:43 +01:00
|
|
|
events to admins when ``DEBUG=False``. Requests resulting in a 400 response due
|
|
|
|
to a ``SuspiciousOperation`` will not be logged to the ``django.request``
|
|
|
|
logger, but only to the ``django.security`` logger.
|
2013-05-16 01:14:28 +02:00
|
|
|
|
2015-03-23 23:17:43 +01:00
|
|
|
To silence a particular type of ``SuspiciousOperation``, you can override that
|
2014-08-15 19:58:50 +02:00
|
|
|
specific logger following this example:
|
2013-05-16 01:14:28 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
.. code-block:: python
|
|
|
|
|
2015-11-07 17:22:30 +01:00
|
|
|
'handlers': {
|
|
|
|
'null': {
|
|
|
|
'class': 'logging.NullHandler',
|
|
|
|
},
|
|
|
|
},
|
2014-08-15 19:58:50 +02:00
|
|
|
'loggers': {
|
|
|
|
'django.security.DisallowedHost': {
|
|
|
|
'handlers': ['null'],
|
|
|
|
'propagate': False,
|
|
|
|
},
|
|
|
|
},
|
2013-05-16 01:14:28 +02:00
|
|
|
|
2016-06-03 02:24:48 +02:00
|
|
|
Other ``django.security`` loggers not based on ``SuspiciousOperation`` are:
|
|
|
|
|
|
|
|
* ``django.security.csrf``: For :ref:`CSRF failures <csrf-rejected-requests>`.
|
|
|
|
|
2014-08-15 14:18:55 +02:00
|
|
|
``django.db.backends.schema``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Logs the SQL queries that are executed during schema changes to the database by
|
|
|
|
the :doc:`migrations framework </topics/migrations>`. Note that it won't log the
|
|
|
|
queries executed by :class:`~django.db.migrations.operations.RunPython`.
|
2016-03-14 11:38:38 +01:00
|
|
|
Messages to this logger have ``params`` and ``sql`` in their extra context (but
|
|
|
|
unlike ``django.db.backends``, not duration). The values have the same meaning
|
|
|
|
as explained in :ref:`django-db-logger`.
|
|
|
|
|
2010-10-04 17:12:39 +02:00
|
|
|
Handlers
|
|
|
|
--------
|
|
|
|
|
|
|
|
Django provides one log handler in addition to those provided by the
|
|
|
|
Python logging module.
|
|
|
|
|
2019-02-24 16:10:14 +01:00
|
|
|
.. class:: AdminEmailHandler(include_html=False, email_backend=None, reporter_class=None)
|
2010-10-04 17:12:39 +02:00
|
|
|
|
2017-11-21 11:21:31 +01:00
|
|
|
This handler sends an email to the site :setting:`ADMINS` for each log
|
2010-10-04 17:12:39 +02:00
|
|
|
message it receives.
|
|
|
|
|
2011-03-16 05:13:57 +01:00
|
|
|
If the log record contains a ``request`` attribute, the full details
|
2016-06-22 12:38:34 +02:00
|
|
|
of the request will be included in the email. The email subject will
|
2015-11-11 09:26:07 +01:00
|
|
|
include the phrase "internal IP" if the client's IP address is in the
|
|
|
|
:setting:`INTERNAL_IPS` setting; if not, it will include "EXTERNAL IP".
|
2010-10-04 17:12:39 +02:00
|
|
|
|
|
|
|
If the log record contains stack trace information, that stack
|
2011-04-01 18:10:22 +02:00
|
|
|
trace will be included in the email.
|
2011-03-16 05:13:57 +01:00
|
|
|
|
|
|
|
The ``include_html`` argument of ``AdminEmailHandler`` is used to
|
2011-04-01 18:10:22 +02:00
|
|
|
control whether the traceback email includes an HTML attachment
|
2011-03-16 05:13:57 +01:00
|
|
|
containing the full content of the debug Web page that would have been
|
2011-05-29 19:41:04 +02:00
|
|
|
produced if :setting:`DEBUG` were ``True``. To set this value in your
|
2011-03-16 05:13:57 +01:00
|
|
|
configuration, include it in the handler definition for
|
2014-08-15 19:58:50 +02:00
|
|
|
``django.utils.log.AdminEmailHandler``, like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2011-03-16 05:13:57 +01:00
|
|
|
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler',
|
|
|
|
'include_html': True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-04-01 18:10:22 +02:00
|
|
|
Note that this HTML version of the email contains a full traceback,
|
2011-03-16 05:13:57 +01:00
|
|
|
with names and values of local variables at each level of the stack, plus
|
|
|
|
the values of your Django settings. This information is potentially very
|
2011-04-01 18:10:22 +02:00
|
|
|
sensitive, and you may not want to send it over email. Consider using
|
2012-11-13 22:51:50 +01:00
|
|
|
something such as `Sentry`_ to get the best of both worlds -- the
|
2011-03-16 05:13:57 +01:00
|
|
|
rich information of full tracebacks plus the security of *not* sending the
|
2011-06-09 00:18:46 +02:00
|
|
|
information over email. You may also explicitly designate certain
|
|
|
|
sensitive information to be filtered out of error reports -- learn more on
|
|
|
|
:ref:`Filtering error reports<filtering-error-reports>`.
|
2011-03-16 05:13:57 +01:00
|
|
|
|
2012-11-19 18:57:40 +01:00
|
|
|
By setting the ``email_backend`` argument of ``AdminEmailHandler``, the
|
|
|
|
:ref:`email backend <topic-email-backends>` that is being used by the
|
2014-08-15 19:58:50 +02:00
|
|
|
handler can be overridden, like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2012-11-19 18:57:40 +01:00
|
|
|
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler',
|
|
|
|
'email_backend': 'django.core.mail.backends.filebased.EmailBackend',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
By default, an instance of the email backend specified in
|
|
|
|
:setting:`EMAIL_BACKEND` will be used.
|
|
|
|
|
2019-02-24 16:10:14 +01:00
|
|
|
The ``reporter_class`` argument of ``AdminEmailHandler`` allows providing
|
|
|
|
an ``django.views.debug.ExceptionReporter`` subclass to customize the
|
|
|
|
traceback text sent in the email body. You provide a string import path to
|
|
|
|
the class you wish to use, like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler',
|
|
|
|
'include_html': True,
|
|
|
|
'reporter_class': 'somepackage.error_reporter.CustomErrorReporter'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
.. versionadded:: 3.0
|
|
|
|
|
|
|
|
The ``reporter_class`` argument was added.
|
|
|
|
|
2014-11-10 22:21:31 +01:00
|
|
|
.. method:: send_mail(subject, message, *args, **kwargs)
|
|
|
|
|
|
|
|
Sends emails to admin users. To customize this behavior, you can
|
|
|
|
subclass the :class:`~django.utils.log.AdminEmailHandler` class and
|
|
|
|
override this method.
|
|
|
|
|
2018-04-18 00:19:29 +02:00
|
|
|
.. _Sentry: https://pypi.org/project/sentry/
|
2011-06-22 08:01:44 +02:00
|
|
|
|
|
|
|
Filters
|
|
|
|
-------
|
|
|
|
|
2017-08-21 22:06:38 +02:00
|
|
|
Django provides some log filters in addition to those provided by the Python
|
2011-09-16 18:41:38 +02:00
|
|
|
logging module.
|
2011-06-22 08:01:44 +02:00
|
|
|
|
|
|
|
.. class:: CallbackFilter(callback)
|
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
This filter accepts a callback function (which should accept a single
|
|
|
|
argument, the record to be logged), and calls it for each record that
|
|
|
|
passes through the filter. Handling of that record will not proceed if the
|
|
|
|
callback returns False.
|
|
|
|
|
|
|
|
For instance, to filter out :exc:`~django.http.UnreadablePostError`
|
|
|
|
(raised when a user cancels an upload) from the admin emails, you would
|
|
|
|
create a filter function::
|
2011-06-22 08:01:44 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
from django.http import UnreadablePostError
|
2012-08-21 15:01:11 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
def skip_unreadable_post(record):
|
|
|
|
if record.exc_info:
|
|
|
|
exc_type, exc_value = record.exc_info[:2]
|
|
|
|
if isinstance(exc_value, UnreadablePostError):
|
|
|
|
return False
|
|
|
|
return True
|
2012-08-21 15:01:11 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
and then add it to your logging config:
|
2012-08-21 15:01:11 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
.. code-block:: python
|
2012-08-21 15:01:11 +02:00
|
|
|
|
|
|
|
'filters': {
|
|
|
|
'skip_unreadable_posts': {
|
|
|
|
'()': 'django.utils.log.CallbackFilter',
|
|
|
|
'callback': skip_unreadable_post,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'filters': ['skip_unreadable_posts'],
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-09-16 18:41:38 +02:00
|
|
|
.. class:: RequireDebugFalse()
|
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
This filter will only pass on records when settings.DEBUG is False.
|
2011-09-16 18:41:38 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
This filter is used as follows in the default :setting:`LOGGING`
|
|
|
|
configuration to ensure that the :class:`AdminEmailHandler` only sends
|
|
|
|
error emails to admins when :setting:`DEBUG` is ``False``:
|
2011-06-22 08:01:44 +02:00
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
'filters': {
|
2011-06-22 08:01:44 +02:00
|
|
|
'require_debug_false': {
|
2011-09-16 18:41:38 +02:00
|
|
|
'()': 'django.utils.log.RequireDebugFalse',
|
2011-06-22 08:01:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'filters': ['require_debug_false'],
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler'
|
|
|
|
}
|
|
|
|
},
|
2012-09-24 22:30:38 +02:00
|
|
|
|
2012-09-26 19:56:21 +02:00
|
|
|
.. class:: RequireDebugTrue()
|
|
|
|
|
2014-08-15 19:58:50 +02:00
|
|
|
This filter is similar to :class:`RequireDebugFalse`, except that records are
|
|
|
|
passed only when :setting:`DEBUG` is ``True``.
|
2012-09-26 19:56:21 +02:00
|
|
|
|
2012-09-24 22:30:38 +02:00
|
|
|
.. _default-logging-configuration:
|
|
|
|
|
|
|
|
Django's default logging configuration
|
|
|
|
======================================
|
|
|
|
|
2015-03-21 18:59:23 +01:00
|
|
|
By default, Django configures the following logging:
|
|
|
|
|
|
|
|
When :setting:`DEBUG` is ``True``:
|
|
|
|
|
2017-08-22 22:24:18 +02:00
|
|
|
* The ``django`` logger sends messages in the ``django`` hierarchy (except
|
|
|
|
``django.server``) at the ``INFO`` level or higher to the console.
|
2015-03-21 18:59:23 +01:00
|
|
|
|
|
|
|
When :setting:`DEBUG` is ``False``:
|
2012-09-24 22:30:38 +02:00
|
|
|
|
2017-08-22 22:24:18 +02:00
|
|
|
* The ``django`` logger sends messages in the ``django`` hierarchy (except
|
|
|
|
``django.server``) with ``ERROR`` or ``CRITICAL`` level to
|
2015-03-23 23:17:43 +01:00
|
|
|
:class:`AdminEmailHandler`.
|
|
|
|
|
2015-11-06 17:19:41 +01:00
|
|
|
Independent of the value of :setting:`DEBUG`:
|
|
|
|
|
2017-08-22 22:24:18 +02:00
|
|
|
* The :ref:`django-server-logger` logger sends messages at the ``INFO`` level
|
|
|
|
or higher to the console.
|
2015-11-06 17:19:41 +01:00
|
|
|
|
2018-11-28 03:13:21 +01:00
|
|
|
All loggers except :ref:`django-server-logger` propagate logging to their
|
|
|
|
parents, up to the root ``django`` logger. The ``console`` and ``mail_admins``
|
|
|
|
handlers are attached to the root logger to provide the behavior described
|
|
|
|
above.
|
|
|
|
|
2012-09-26 19:56:21 +02:00
|
|
|
See also :ref:`Configuring logging <configuring-logging>` to learn how you can
|
2019-05-07 21:12:58 +02:00
|
|
|
complement or replace this default logging configuration defined in
|
|
|
|
:source:`django/utils/log.py`.
|