1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Made logging config examples more accessible.

- Show an initial example configuring the root logger to output to the console.
- Then add more logging from the `django` named logger.
- Then show the file and more complex examples.

Adjusted surrounding text for reading flow.

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
This commit is contained in:
Ben Li-Sauerwine 2020-03-17 06:51:05 -04:00 committed by GitHub
parent 0538da08c5
commit fc84848cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -238,8 +238,73 @@ 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.
First, here's a configuration which writes all logging from the
:ref:`django-logger` logger to a local file:
To begin, here's a small configuration that will allow you to output all log
messages to the console:
.. code-block:: python
:caption: settings.py
import os
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'root': {
'handlers': ['console'],
'level': 'WARNING',
},
}
This configures the parent ``root`` logger to send messages with the
``WARNING`` level and higher to the console handler. By adjusting the level to
``INFO`` or ``DEBUG`` you can display more messages. This may be useful during
development.
Next we can add more fine-grained logging. Here's an example of how to make the
logging system print more messages from just the :ref:`django-logger` named
logger:
.. code-block:: python
:caption: settings.py
import os
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'root': {
'handlers': ['console'],
'level': 'WARNING',
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
'propagate': False,
},
},
}
By default, this config sends messages from the ``django`` logger of level
``INFO`` or higher to the console. This is the same level as Django's default
logging config, except that the default config only displays log records when
``DEBUG=True``. Django does not log many such ``INFO`` level messages. With
this config, however, you can also set the environment variable
``DJANGO_LOG_LEVEL=DEBUG`` to see all of Django's debug logging which is very
verbose as it includes all database queries.
You don't have to log to the console. Here's a configuration which writes all
logging from the :ref:`django-logger` named logger to a local file:
.. code-block:: python
:caption: settings.py
@ -266,37 +331,6 @@ First, here's a configuration which writes all logging from the
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.
Second, here's an example of how to make the logging system print Django's
logging to the console. It may be useful during local development.
By default, this config only sends messages of level ``INFO`` or higher to the
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
``DJANGO_LOG_LEVEL=DEBUG`` to see all of Django's debug logging which is very
verbose as it includes all database queries:
.. code-block:: python
:caption: settings.py
import os
LOGGING = {
'version': 1,
'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:
.. code-block:: python