为什么logger使用yaml配置只生成文件而不生成输出

2024-04-29 12:23:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

enter image description here

__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。但是文件是空的。没有输出到这个文件。甚至“已完成配置logger()!”在__init__.py

为什么不打印到日志文件


Tags: pathlogconfigyamlosformatterlogginghandlers