具有父记录器的记录器如何将effectiveLevel设置为0?

时间:2014-08-01 03:09:45

标签: python logging

使用日志记录继承,并且稍微混淆了我看到的有效日志级别。

我的文件结构如下:

project/
--> __init.py__
--> some_folder/
----> some_file.py

__init.py__中,我从yaml文件中的dict配置创建根记录器。我记录它的effectiveLevel并且它是预期的(在yaml文件中设置为INFO)。但是,我在some_file.py中记录了记录器的effectiveLevel,并且结果为0.我意识到非root记录器默认为level NOTSET(0),但我希望effectiveLevel与root logger相同,所以INFO而不是0。

知道为什么会这样吗?

EDIT添加了创建记录器的代码:

YAML:

  version: 1
  disable_existing_loggers: False
  formatters:
      simple:
      format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

  handlers:
      console:
      class: logging.StreamHandler
      level: DEBUG
      formatter: simple
      stream: ext://sys.stdout

  root:
      level: INFO
      handlers: [console]
      propagate: no

配置膨胀:

with open('path_to_yaml', 'r') as f:
    config = yaml.load(f.read())
    logging.config.dictConfig(config)

1 个答案:

答案 0 :(得分:0)

看起来我错误地假设它会继承root的级别。看起来我需要添加一个名为项目的记录器来完成预期的行为。

  version: 1
  disable_existing_loggers: False
  formatters:
    simple:
      format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

  handlers:
    console:
      class: logging.StreamHandler
      level: DEBUG
      formatter: simple
      stream: ext://sys.stdout

  loggers:
    project:
      level: INFO
      handlers: [console]
      propagate: no

  root:
      level: INFO
      handlers: [console]