python / python/cpython

MemoryHandler can't be setup through a DictConfig if any other logger is setup after it

未关闭
#123,239 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

This is the logging that's failing.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "fine-logger1": {
            'level': logging.INFO,
            'formatter': 'verbose-console',
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',
        },
        "fine-logger2": {
            "level": logging.INFO,
            "class": "logging.FileHandler",
            "filename": Path("logs/somefilename.log"),
            "mode": "a",
            "formatter": "verbose",
        },
        "fine-logger3": {
            "level": logging.DEBUG,
            "class": "logging.FileHandler",
            "filename": Path("logs/errors/somefilename.log"),
            "delay": True,
            "formatter": "verbose",
        },
        "bugged-logger3": {
            "level": logging.DEBUG,
            "class": "logging.handlers.MemoryHandler",
            "capacity": 9999999,
            "flushLevel": logging.ERROR,
            "target": "fine-logger3",
            "flushOnClose": False,
        },
    },
    "loggers": {
        "bug-logger": {
            "handlers": ["fine-logger1", "fine-logger2", "bugged-logger3"],
            "level": logging.DEBUG,
            "propagate": True
        },
    },
}

After it's been setup, if any other handler is setup afterwards (in my case it's the copernicusmarine logger) the handler bugged-logger3 will have a None inside its target field, as every handler that's not incremental will delete existing unused handlers.

I've dug into the source code of logging and I think I've tracked down where this is happpening, but I'm not really sure on how to fix it.
In the config file python3.11/logging/config.py in the configure() function there's a call to _clearExistingHandlers():

            else:
                disable_existing = config.pop('disable_existing_loggers', True)

                _clearExistingHandlers() <-----

                # Do formatters first - they don't refer to anything else
                formatters = config.get('formatters', EMPTY_DICT)
                for name in formatters:
                    try:
                        formatters[name] = self.configure_formatter(
                                                            formatters[name])

If we comment this line, it works completely fine. But I guess this just keeps handlers loaded in memory and that's not a proper solution.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 python3.11/logging/config.py 开始,重点查看 DictConfig.configure() 和 _clearExistingHandlers()。复现提供的配置,然后配置另一个 logger,并检查 MemoryHandler.target 是否会变为 None。完成的标准是:后续的非增量配置不再破坏 MemoryHandler 的 target,同时 handler 清理仍能正确执行。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。