简体   繁体   English

在python中配置root logger

[英]Configuring root logger in python

I have the following logging configuration in in my Django settings. 我在Django设置中有以下日志记录配置。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
         '': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
         },
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

With this configuration I expect my 'apps' to log at DEBUG level and any other modules to log only ERROR and above. 通过这种配置,我希望我的'apps'能够以DEBUG级别登录,而任何其他模块只能记录ERROR及以上级别。 But I see DEBUG messages from other modules. 但我看到来自其他模块的DEBUG消息。 How do I fix it? 我如何解决它?

Are you using an empty string key in LOGGING['loggers'] to match the root logger? 您是否在LOGGING['loggers']使用空字符串键来匹配根记录器? If so, you could try this instead. 如果是这样,你可以试试这个。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        }
    },
    'root': {
       'handlers': ['console'],
       'level': 'ERROR'
    }
}

I had a similar issue, with root logger configured a level at INFO but seeing DEBUG log message. 我遇到了类似的问题,root logger在INFO配置了一个级别,但是看到了DEBUG日志消息。

Turns out I should not set 'propagate': True for my other logger which is at level DEBUG , since that those logs will be passed to the root logger no matter what level root at. 事实证明我不应该设置'propagate': True对于我在DEBUG级别的其他记录器是'propagate': True ,因为那些日志将被传递到根记录器,无论根目录是什么级别。

So my guess here to the original question is that there might be some other modules' logger with propagate turned on. 所以我对原始问题的猜测是,可能有一些其他模块的记录器已打开propagate set disable_existing_loggers to True maybe solve this. disable_existing_loggers设置为True可能会解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM