导入时记录器命名与日志记录配置不兼容 [英] Incompatibility between import-time logger naming with logging configuration

查看:81
本文介绍了导入时记录器命名与日志记录配置不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过读取文件并使用 fileConfig 选项。我希望能够在测试和实时日志记录配置之间切换,所以我想先读取一个单独的配置文件,然后从那里提取日志配置文件路径。

I am setting up my Python logging in main.py via reading in a file and using the fileConfig option. I want to be able to switch between testing and live logging configurations, so I want to read in a separate config file first and extract the logging config file path from there.

这里的问题是我从main.py导入的其他文件通过 log = getLogger(__ name __)获取自己的记录器,这在导入时发生。这些链接在加载新配置时会被破坏,并且这些模块最终没有按照我期望的方式运行日志。

The problem here is that other files that I import from main.py grab their own logger via log = getLogger(__name__), and this happens at import time. These links then get broken when the new configuration is loaded in, and these modules end up without the logging working the way I expect.

我不能轻易地延迟导入这些模块没有很多重构,所以有没有其他方法可以保持这种方法按模块名称设置记录器,同时仍然在以后加载日志配置?

I can't easily delay the importing of these modules without a lot of refactoring, so is there any other way of being able to keep this method of setting up loggers by module name while still loading in the log configuration later?

推荐答案

我不确定你的问题究竟是怎么破坏的,但这就是我看到它的方式。执行 log = logging.getLogger(__ name __)的各种模块将具有其记录器的有效名称(记录器名称=包名称),除非您以某种方式实际移动模块到某些其他软件包位置。

I'm not sure from your question exactly how things are breaking, but here's how I see it. The various modules which do log = logging.getLogger(__name__) will have valid names for their loggers (logger name = package name), unless you were to somehow actually move the modules to some other package location.

在导入时,日志配置可能已设置也可能未设置,并且不应进行任何实际的日志记录调用 - 导入的影响(如果有的话,消息可能无处可去)。

At import time, the logging configuration may or may not have been set, and there shouldn't be any actual logging calls made as a side-effect of the import (if there are, the messages may have nowhere to go).

使用 fileConfig 通常只在记录器上设置处理程序,格式化程序和级别。

Loading a new configuration using fileConfig typically just sets handlers, formatters and levels on loggers.

当您随后在导入的模块中调用代码时,它们通过其记录器进行记录,这些记录器具有附加的处理程序您以前的配置调用 - 因此它们将根据配置输出。

When you subsequently call code in the imported modules, they log via their loggers, which have handlers attached by your previous configuration call - so they will output according to the configuration.

您应该知道在旧版本的Python(< = 2.5)上,调用 fileConfig 将不可避免地禁用不是na的现有记录器med在配置中 - 在更新版本的Python(> = 2.6)中,可以使用传递给 fileConfig <的 disable_existing_loggers = False 关键字参数进行配置/ code>。您可能需要检查这一点,因为它有时会导致意外行为(该参数的默认值为 True ,以便与旧版Python下的行为兼容)。

You should be aware that on older versions of Python (<= 2.5), calls to fileConfig would unavoidably disable existing loggers which weren't named in the configuration - in more recent versions of Python (>= 2.6), this is configurable using a disable_existing_loggers=False keyword argument passed to fileConfig. You may want to check this, as it sometimes leads to unexpected behaviour (the default for that parameter is True, for compatibility with behaviour under the older Python versions).

如果你发布更多关于看似破坏的细节,我或许可以更好地诊断出现了什么。

If you post more details about what seems broken, I might be able to provide a better diagnosis of what's going on.

这篇关于导入时记录器命名与日志记录配置不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆