首页 > 解决方案 > 无法在 python 中设置日志记录级别

问题描述

以下代码和 yaml 配置生成警告消息,但不生成调试消息,从 Eclipse/Pydev 或命令行运行。未注释 print 语句,配置字典看起来不错。我错过了什么?

setupLogging.py:

#!/usr/bin/env python3

import logging.config
import yaml

def setupLogging():
    with open('loggingConfig.yaml', 'rt') as file:
        config = yaml.safe_load(file.read())
        #print(str(config))
        logging.config.dictConfig(config)

if __name__ == "__main__":
    setupLogging()
    logger = logging.getLogger(__name__)
    logger.debug("logger debug")
    logger.warning("logger warning")

日志配置.yaml:

---

version: 1

disable_existing_loggers: False

formatters:

    simple:
        format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: simple
        stream: ext://sys.stdout

root:
    handlers: [console]

...

标签: pythonlogging

解决方案


推荐阅读