logging.config.dictConfig 的完整示例在哪里? [英] Where is a complete example of logging.config.dictConfig?

查看:29
本文介绍了logging.config.dictConfig 的完整示例在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 dictConfig,但文档是有点抽象.我在哪里可以找到与 dictConfig 一起使用的字典的具体的、可复制+粘贴的示例?

I'd like to use dictConfig, but the documentation is a little bit abstract. Where can I find a concrete, copy+paste-able example of the dictionary used with dictConfig?

推荐答案

这里怎么样!相应的文档参考是 configuration-dictionary-schema.

How about here! The corresponding documentation reference is configuration-dictionary-schema.

LOGGING_CONFIG = { 
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': { 
        'standard': { 
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': { 
        'default': { 
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',  # Default is stderr
        },
    },
    'loggers': { 
        '': {  # root logger
            'handlers': ['default'],
            'level': 'WARNING',
            'propagate': False
        },
        'my.packg': { 
            'handlers': ['default'],
            'level': 'INFO',
            'propagate': False
        },
        '__main__': {  # if __name__ == '__main__'
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': False
        },
    } 
}

用法:

import logging.config

# Run once at startup:
logging.config.dictConfig(LOGGING_CONFIG)

# Include in each module:
log = logging.getLogger(__name__)
log.debug("Logging is configured.")

如果您看到太多来自第三方包的日志,请确保在第三方包之前使用 logging.config.dictConfig(LOGGING_CONFIG) 运行此配置包被导入.

In case you see too many logs from third-party packages, be sure to run this config using logging.config.dictConfig(LOGGING_CONFIG) before the third-party packages are imported.

要使用日志过滤器向每条日志消息添加额外的自定义信息,请考虑这个答案.

To add additional custom info to each log message using a logging filter, consider this answer.

这篇关于logging.config.dictConfig 的完整示例在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆