将多个处理程序传递给记录器

时间:2019-03-15 13:10:08

标签: python logging

我有一个日志记录的.yaml配置文件(局部):

handlers:
  console:
    class: logging.StreamHandler
    stream: ext://sys.stderr
    formatter: basic
  console_file: 
    class: logging.handlers.RotatingFileHandler
    filename: bvbot_general.log
    maxBytes: 20000000
    backupCount: 3
    encoding: utf-8
    formatter: basic

loggers:
  general:
    handlers: [console]
    level: DEBUG
    propagate: False
  general_file:
    handlers: [console_file]
    level: DEBUG
    propagate: True

root:
  handlers: [console]
  level: INFO

记录器“ general”和“ general_file”分别按预期工作。但是,如果我将'console_file'处理程序添加到记录器'general'中,则只会输出到控制台,而不是预期的文件:

loggers:
  general:
    handlers: [console, console_file]
    level: DEBUG
    propagate: False

1 个答案:

答案 0 :(得分:1)

如何获取记录器?使用以下代码对我来说很好用:

import os
import logging.config

import yaml

# Loading from logging.yaml
# ...

logger = logging.getLogger('general')
logger.info('hello')
相关问题