How to configure and use logging

    Django provides a working default logging configuration that is readily extended.

    To send a log message from within your code, you place a logging call into it.

    Don’t be tempted to use logging calls in settings.py.

    The way that Django logging is configured as part of the setup() function means that logging calls placed in settings.py may not work as expected, because logging will not be set up at that point. To explore logging, use a view function as suggested in the example below.

    First, import the Python logging library, and then obtain a logger instance with . Provide the getLogger() method with a name to identify it and the records it emits. A good option is to use __name__ (see Use logger namespacing below for more on this) which will provide the name of the current Python module as a dotted path:

    It’s a good convention to perform this declaration at module level.

    And then in a function, for example in a view, send a record to the logger:

    1. def some_view(request):
    2. ...
    3. if some_risky_state:
    4. logger.warning('Platform is running at risk')

    When this code is executed, a containing that message will be sent to the logger. If you’re using Django’s default logging configuration, the message will appear in the console.

    The WARNING level used in the example above is one of several logging severity levels: DEBUG, INFO, WARNING, ERROR, CRITICAL. So, another example might be:

    1. logger.critical('Payment system is not responding')

    重要

    Records with a level lower than WARNING will not appear in the console by default. Changing this behavior requires additional configuration.

    Customize logging configuration

    Although Django’s logging configuration works out of the box, you can control exactly how your logs are sent to various destinations - to log files, external services, email and so on - with some additional configuration.

    You can configure:

    • logger mappings, to determine which records are sent to which handlers
    • handlers, to determine what they do with the records they receive
    • filters, to provide additional control over the transfer of records, and even modify records in-place
    • formatters, to convert LogRecord objects to a string or other form for consumption by human beings or another system

    There are various ways of configuring logging. In Django, the setting is most commonly used. The setting uses the dictConfig format, and extends the .

    See 日志模块的配置 for an explanation of how your custom settings are merged with Django’s defaults.

    When configuring logging, it makes sense to

    Create a LOGGING dictionary

    In your settings.py:

    1. LOGGING = {
    2. 'version': 1, # the dictConfig format version
    3. 'disable_existing_loggers': False, # retain the default loggers
    4. }

    It nearly always makes sense to retain and extend the default logging configuration by setting disable_existing_loggers to False.

    Configure a handler

    This example configures a single handler named file, that uses Python’s to save logs of level DEBUG and higher to the file general.log (at the project root):

    1. LOGGING = {
    2. [...]
    3. 'handlers': {
    4. 'file': {
    5. 'class': 'logging.FileHandler',
    6. 'filename': 'general.log',
    7. },
    8. }

    Different handler classes take different configuration options. For more information on available handler classes, see the AdminEmailHandler provided by Django and the various provided by Python.

    Logging levels can also be set on the handlers (by default, they accept log messages of all levels). Using the example above, adding:

    would define a handler configuration that only accepts records of level DEBUG and higher.

    Configure a logger mapping

    To send records to this handler, configure a logger mapping to use it for example:

    1. LOGGING = {
    2. [...]
    3. 'loggers': {
    4. '': {
    5. 'level': 'DEBUG',
    6. },
    7. },
    8. }

    The mapping’s name determines which log records it will process. This configuration ('') is unnamed. That means that it will process records from all loggers (see below on how to use the mapping name to determine the loggers for which it will process records).

    It will forward messages of levels DEBUG and higher to the handler named file.

    Note that a logger can forward messages to multiple handlers, so the relation between loggers and handlers is many-to-many.

    If you execute:

    1. logger.debug('Attempting to connect to API')

    in your code, you will find that message in the file general.log in the root of the project.

    Configure a formatter

    By default, the final log output contains the message part of each . Use a formatter if you want to include additional data. First name and define your formatters - this example defines formatters named verbose and simple:

    1. LOGGING = {
    2. [...]
    3. 'formatters': {
    4. 'verbose': {
    5. 'format': '{name} {levelname} {asctime} {module} {process:d} {thread:d} {message}',
    6. 'style': '{',
    7. },
    8. 'simple': {
    9. 'format': '{levelname} {message}',
    10. 'style': '{',
    11. },
    12. },
    13. }

    The style keyword allows you to specify { for str.format() or $ for formatting; the default is $.

    To apply a formatter to a handler, add a formatter entry to the handler’s dictionary referring to the formatter by name, for example:

    1. 'handlers': {
    2. 'file': {
    3. 'class': 'logging.FileHandler',
    4. 'filename': 'general.log',
    5. 'formatter': 'verbose',
    6. },

    Use logger namespacing

    The unnamed logging configuration '' captures logs from any Python application. A named logging configuration will capture logs only from loggers with matching names.

    The namespace of a logger instance is defined using . For example in views.py of my_app:

    will create a logger in the my_app.views namespace. __name__ allows you to organize log messages according to their provenance within your project’s applications automatically. It also ensures that you will not experience name collisions.

    A logger mapping named my_app.views will capture records from this logger:

    1. LOGGING = {
    2. [...]
    3. 'loggers': {
    4. ...
    5. },
    6. },
    7. }

    A logger mapping named my_app will be more permissive, capturing records from loggers anywhere within the my_app namespace (including my_app.views, my_app.utils, and so on):

    1. LOGGING = {
    2. [...]
    3. 'loggers': {
    4. 'my_app': {
    5. ...
    6. },
    7. },
    8. }

    You can also define logger namespacing explicitly:

    1. logger = logging.getLogger('project.payment')

    and set up logger mappings accordingly.

    Using logger hierarchies and propagation

    Logger naming is hierarchical. my_app is the parent of my_app.views, which is the parent of my_app.views.private. Unless specified otherwise, logger mappings will propagate the records they process to their parents - a record from a logger in the my_app.views.private namespace will be handled by a mapping for both my_app and my_app.views.

    To manage this behavior, set the propagation key on the mappings you define:

    1. LOGGING = {
    2. [...]
    3. 'loggers': {
    4. 'my_app': {
    5. [...]
    6. },
    7. 'my_app.views': {
    8. [...]
    9. },
    10. 'my_app.views.private': {
    11. [...]
    12. 'propagate': False,
    13. },
    14. },

    propagate defaults to True. In this example, the logs from my_app.views.private will not be handled by the parent, but logs from my_app.views will.

    Configure responsive logging

    Logging is most useful when it contains as much information as possible, but not information that you don’t need - and how much you need depends upon what you’re doing. When you’re debugging, you need a level of information that would be excessive and unhelpful if you had to deal with it in production.

    You can configure logging to provide you with the level of detail you need, when you need it. Rather than manually change configuration to achieve this, a better way is to apply configuration automatically according to the environment.

    For example, you could set an environment variable DJANGO_LOG_LEVEL appropriately in your development and staging environments, and make use of it in a logger mapping thus:

    - so that unless the environment specifies a lower log level, this configuration will only forward records of severity WARNING and above to its handler.