具有父记录器的记录器的有效级别如何为0?

2024-09-28 21:00:07 发布

您现在位置:Python中文网/ 问答频道 /正文

玩弄日志继承,对我看到的有效日志级别有点困惑

我的文件结构如下:

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

__init.py__中,我从yaml文件中的dict配置创建根记录器。我记录了它的effectiveLevel,它与预期的一样(在yaml文件中设置为INFO)。但是,我将记录器的effectiveLevel记录在某个_file.py中,结果是0。我意识到非根记录器默认为level NOTSET(0),但我希望effectiveLevel与根记录器相同,因此INFO与0相反

知道为什么吗

编辑添加的创建日志记录器的代码:

亚马尔:

  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)

Tags: 文件pyinfoconfigyamlinithandlers记录
1条回答
网友
1楼 · 发布于 2024-09-28 21:00:07

看起来我错误地假设它将继承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]

相关问题 更多 >