Django Setup Default Logging

后端 未结 4 975
無奈伤痛
無奈伤痛 2020-12-04 05:40

I can\'t seem to figure out how to setup a \"default\" logger for my Django installation. I would like to use Django 1.3\'s new LOGGING setting in setting

4条回答
  •  情深已故
    2020-12-04 06:23

    As you said in your answer, Chris, one option to define a default logger is to use the empty string as its key.

    However, I think the intended way is to define a special logger under the root key of the logging configuration dictionary. I found this in the Python documentation:

    root - this will be the configuration for the root logger. Processing of the configuration will be as for any logger, except that the propagate setting will not be applicable.

    Here's the configuration from your answer changed to use the root key:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'standard': {
                'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
            },
        },
        'handlers': {
            'default': {
                'level':'DEBUG',
                'class':'logging.handlers.RotatingFileHandler',
                'filename': 'logs/mylog.log',
                'maxBytes': 1024*1024*5, # 5 MB
                'backupCount': 5,
                'formatter':'standard',
            },  
            'request_handler': {
                'level':'DEBUG',
                'class':'logging.handlers.RotatingFileHandler',
                'filename': 'logs/django_request.log',
                'maxBytes': 1024*1024*5, # 5 MB
                'backupCount': 5,
                'formatter':'standard',
            },
        },
        'root': {
            'handlers': ['default'],
            'level': 'DEBUG'
        },
        'loggers': {
            'django.request': {
                'handlers': ['request_handler'],
                'level': 'DEBUG',
                'propagate': False
            },
        }
    }
    

    To be fair, I can't see any difference in behaviour between the two configurations. It appears that defining a logger with an empty string key will modify the root logger, because logging.getLogger('') will return the root logger.

    The only reason I prefer 'root' over '' is that it is explicit about modifying the root logger. In case you were curious, 'root' overrides '' if you define both, just because the root entry is processed last.

提交回复
热议问题