python - 为什么使用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
为什么它不打印到日志文件?
解决方案
推荐阅读
- php - PayPal IPN 循环遍历 PHP 中的购物车项目,字符串文字问题
- c# - 在 VS2010 中安装 Microsoft.Data.Analysis
- vbscript - 注册表项中的无效根 (VBScript)
- reactjs - Next.js 静态页面 - json 数据
- docker - 已关闭 - docker 容器上的 Ansible 任务 - 在 PATH 中找不到 Docker 命令
- python - 对半正弦距离的 gps 数据进行分组并计算平均值
- objective-c - 锁定原理——在Objective C中
- flutter - 如何在flutter中从网络图像创建pdf
- keras - 根据论文实现EEGNet卷积神经网络的问题
- php - 在更新 Laravel 时从空值创建默认对象