为什么logger只使用yaml配置生成文件但没有输出

时间:2018-05-18 03:23:15

标签: python logging yaml

我正在使用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()!')

logging.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

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

0 个答案:

没有答案