django.request logger not propagated to root?

后端 未结 3 638
梦谈多话
梦谈多话 2020-12-23 04:14

Using Django 1.5.1:

DEBUG = False

LOGGING = {
    \'version\': 1,
    \'disable_existing_loggers\': True,
    \'formatters\': {
        \'verbose\': {
               


        
3条回答
  •  春和景丽
    2020-12-23 04:31

    For Django-2.1 I found that the logging configuration is more concise:

    $ ./manage.py shell
    
    >>> import logging
    >>> # Grub all Django loggers
    >>> loggers = [
            name for name in logging.root.manager.loggerDict 
            if 'django' in name
        ]
    >>> for each in loggers:
            logger = logging.getLogger(each)
            print(
                'Logger Name: {0}\nLogger Handlers: {1}\n'
                'Logger Propagates: {2}\n\n'.format(
                    each, 
                    logger.handlers, 
                    logger.propagate
                )
            )
    
    Logger Name: django.db
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.request
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.template
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.db.backends
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.db.backends.schema
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.security.csrf
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django
    Logger Handlers: [, ]
    Logger Propagates: True
    
    
    Logger Name: django.contrib.gis
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.contrib
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.security
    Logger Handlers: []
    Logger Propagates: True
    
    
    Logger Name: django.server
    Logger Handlers: []
    Logger Propagates: False
    

    As quoted in the docs:

    All loggers except django.server 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.

    This is in accordance with the docs of propagate which state that:

    Note

    If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers, provided that their propagate setting is left set to True. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.

    Therefore I decided not to prevent Django from configuring logging. I wanted to stop sending emails to the admins because I use sentry, and I just configured the root logger to use the console and file handlers, according to the django docs examples:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'verbose': {
                'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
                'style': '{',
            },
            'simple': {
                'format': '{levelname} {message}',
                'style': '{',
            },
        },
        'filters': {
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse',
            },
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {
            'console': {
                'level': 'INFO',
                'filters': ['require_debug_true'],
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            'file': {
                'level': 'INFO',
                'filters': ['require_debug_false'],
                'class': 'logging.FileHandler',
                'filename': os.path.join(LOGGING_DIR, 'django.log'),
                'formatter': 'verbose'
            },
        },
        'loggers': {
            'django': {
                'handlers': ['file', 'console'],
                'level': 'INFO',
                'propagate': True,
            },
        }
    }
    

    Which results in:

    Logger Name: django
    Logger Handlers: [, ]
    Logger Propagates: True
    

    Not tested in production yet, but it seems that it will work as expected.

提交回复
热议问题