首页 > 解决方案 > 为什么使用yaml配置logger只生成文件不输出

问题描述

我正在使用 Yaml 为我的 python 应用程序设置记录器。

在 中完成实际配置时app/__init__.py,从 中读取配置信息logging.yaml

在此处输入图像描述

__init__.py

import os
import logging.config
import yaml

config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'logging.yaml')
if os.path.exists(config_path):
    with open(config_path, 'rt') as f:
        config = yaml.safe_load(f.read())
    logging.config.dictConfig(config)
else:
    logging.basicConfig(level=logging.INFO)

# Log that the logger was configured
logger = logging.getLogger(__name__)
logger.info('Completed configuring logger()!')

日志记录.yaml

version: 1
disable_existing_loggers: False

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

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

    info_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: INFO
        formatter: standard
        filename: aae.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    error_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: ERROR
        formatter: standard
        filename: aae.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    debug_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: DEBUG
        formatter: standard
        filename: aae.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8


    warn_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: WARN
        formatter: standard
        filename: aae.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

loggers:

    app.run:
        level: DEBUG
        handlers: [info_file_handler, error_file_handler, debug_file_handler]
        propagate: no

所有级别都应该记录到文件aae.log中。当我运行应用程序run.py时,我确实看到aae.log生成了。但是文件是空的。没有任何东西可以输出到这个文件。甚至没有“完成配置记录器()!” 在__init__.py

为什么它不打印到日志文件?

标签: pythonloggingyaml

解决方案


推荐阅读