python - Python Logging:记录器写入两个日志文件而不是一个
问题描述
我正在尝试使用 yaml 配置为两个不同的文件设置两个不同的处理程序。但是,当我调用单个记录器时,它不会写入处理程序中指定的文件,而是写入两个处理程序中提到的两个文件。
配置.yaml
version: 1
disable_existing_loggers: False
formatters:
simple:
format: '%(asctime)s : %(levelname)s : %(filename)s : %(lineno)d : %(message)s'
handlers:
jdbc_handler:
class: logging.handlers.RotatingFileHandler
filename: logs/jdbc.logs
level: DEBUG
formatter: simple
api_handler:
class: logging.handlers.RotatingFileHandler
filename: logs/api.logs
level: DEBUG
formatter: simple
loggers:
jdbc:
level: DEBUG
handler: [jdbc_handler]
api:
level: DEBUG
handler: [api_handler]
root:
level: DEBUG
handlers: [jdbc_handler, api_handler]
主文件
import logging
import yaml
from logging.config import dictConfig
path = "logs/config.yaml"
if os.path.exists(path):
with open(path, 'rt') as file:
config = yaml.safe_load(file.read())
dictConfig(config)
logger = logging.getLogger('jdbc')
我希望它只将日志写入 jdbc.logs 文件,而是将所有 INFO 和 DEBUG 写入 jdbc.logs 和 api.logs。我在这里想念什么?
解决方案
推荐阅读
- python-3.x - 使用 FlaskForm 进行错误检查和闪烁错误消息
- mongodb - Istio 特使代理问题
- reactjs - 是否可以在调用 REST api 时使用 if 语句?
- javascript - 如何使用 Vue.js 设置文本节点的样式?
- node.js - 错误:找不到模块 \node_modules\sqlite3\lib\binding\electron-v8.0-win32-x64\node_sqlite3.node'
- python - Python 3 CSV writer 拆分包含逗号的行
- r - 如何过滤以前大于特定值的时间序列中的值
- python - 查找数组的numpy数组的特征值
- java - 尝试在 java 中读取文本文件 - 错误:目录文件不存在 - LINUX
- mysql - 登录 PhpMyAdmin 时出现错误代码 200