首页 > 解决方案 > 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。我在这里想念什么?

标签: pythonloggingpython-logging

解决方案


推荐阅读