gpt4 book ai didi

python - 日志记录在 python 3.9.4 中似乎无法正常工作

转载 作者:行者123 更新时间:2023-12-03 01:23:54 24 4
gpt4 key购买 nike

目标:

在我的 python 项目的多个文件/模块中使用日志记录模块。这些日志应全部保存到一个日志文件中。

问题:

Python 3.9.4 中的日志记录模块似乎仅适用于一个文件中的日志记录对象。命名过程,即模块命名似乎不太有效。

问题

在“示例”部分中,日志记录模块应该以这种方式工作吗?我可以理解您是否需要定义“根”或“初始”记录器(最后一个示例不会成为问题)。这对我来说似乎不正确。我错了吗?

示例

我完全按照日志记录手册 here 中的方式复制了代码。 。我使用了第一个示例“在多个模块中使用日志记录”

当我剪切并粘贴代码时,代码运行没有错误。我添加了一个新文件只是为了测试并且代码运行良好。请参阅下面与原始运行相匹配的结果。

2021-05-18 20:13:56,270 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2021-05-18 20:13:56,270 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2021-05-18 20:13:56,270 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - doing something
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2021-05-18 20:13:56,270 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2021-05-18 20:13:56,270 - spam_application - INFO - calling auxiliary_module.some_function()
2021-05-18 20:13:56,270 - spam_application.auxiliary - INFO - received a call to "some_function"
2021-05-18 20:13:56,270 - spam_application - INFO - done with auxiliary_module.some_function()

下面是new_file.py中的代码->

import logging

nf_logger = logging.getLogger('spam_application.new_file')

def test_logging():
nf_logger.info('in test_logging()')

下面是 new_file.py 的结果,效果很好。

2021-05-18 20:13:56,270 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2021-05-18 20:13:56,270 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2021-05-18 20:13:56,270 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - doing something
2021-05-18 20:13:56,270 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2021-05-18 20:13:56,270 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2021-05-18 20:13:56,270 - spam_application - INFO - calling auxiliary_module.some_function()
2021-05-18 20:13:56,270 - spam_application.auxiliary - INFO - received a call to "some_function"
2021-05-18 20:13:56,270 - spam_application - INFO - done with auxiliary_module.some_function()
2021-05-18 20:13:56,270 - spam_application - INFO - before test_logging
2021-05-18 20:13:56,270 - spam_application.new_file - INFO - in test_logging()
2021-05-18 20:13:56,270 - spam_application - INFO - after test_logging

这就是奇怪的部分出现的地方。如果我将 auxilary_module.py 中的代码替换为 module_logger = logging.getLogger('spam_application.auxiliary')module_logger = logging.getLogger('__name__')我得到以下结果 ->

2021-05-18 20:20:02,233 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2021-05-18 20:20:02,233 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2021-05-18 20:20:02,233 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2021-05-18 20:20:02,233 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2021-05-18 20:20:02,233 - spam_application.auxiliary.Auxiliary - INFO - doing something
2021-05-18 20:20:02,233 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2021-05-18 20:20:02,234 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2021-05-18 20:20:02,234 - spam_application - INFO - calling auxiliary_module.some_function()
2021-05-18 20:20:02,234 - spam_application - INFO - done with auxiliary_module.some_function()
2021-05-18 20:20:02,234 - spam_application - INFO - before test_logging
2021-05-18 20:20:02,234 - spam_application.new_file - INFO - in test_logging()
2021-05-18 20:20:02,234 - spam_application - INFO - after test_logging

我错过了这一行 <date-time stamp> - spam_application.auxiliary - INFO - received a call to "some_function"

如果我替换nf_logger = logging.getLogger('spam_application.new_file')nf_logger = logging.getLogger(__name__)我得到以下结果 ->

2021-05-18 20:23:09,820 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2021-05-18 20:23:09,820 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2021-05-18 20:23:09,820 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2021-05-18 20:23:09,820 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2021-05-18 20:23:09,820 - spam_application.auxiliary.Auxiliary - INFO - doing something
2021-05-18 20:23:09,820 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2021-05-18 20:23:09,820 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2021-05-18 20:23:09,820 - spam_application - INFO - calling auxiliary_module.some_function()
2021-05-18 20:23:09,820 - spam_application - INFO - done with auxiliary_module.some_function()
2021-05-18 20:23:09,820 - spam_application - INFO - before test_logging
2021-05-18 20:23:09,820 - spam_application - INFO - after test_logging

我缺少日志行 <datetime stamp> - spam_application - INFO - done with auxiliary_module.some_function() .

如果我替换logger = logging.getLogger('spam_application')logger = logging.getLogger(__name__)我得到以下结果 ->

2021-05-18 20:24:50,729 - __main__ - INFO - creating an instance of auxiliary_module.Auxiliary
2021-05-18 20:24:50,729 - __main__ - INFO - created an instance of auxiliary_module.Auxiliary
2021-05-18 20:24:50,729 - __main__ - INFO - calling auxiliary_module.Auxiliary.do_something
2021-05-18 20:24:50,729 - __main__ - INFO - finished auxiliary_module.Auxiliary.do_something
2021-05-18 20:24:50,729 - __main__ - INFO - calling auxiliary_module.some_function()
2021-05-18 20:24:50,729 - __main__ - INFO - done with auxiliary_module.some_function()
2021-05-18 20:24:50,729 - __main__ - INFO - before test_logging
2021-05-18 20:24:50,729 - __main__ - INFO - after test_logging

我缺少 5 行日志。未调用类对象中的日志记录。

在高级日志记录教程中here一个好的约定是使用 logger = logging.getLogger(__name__) 。这在 python 3.9.4 中似乎不起作用。

最佳答案

记录器存在于层次结构中。每个记录器都有自己的级别。根记录器默认级别为警告

hierarchy这样以点分隔的名称可以告诉您 logger 属于其中的位置。名为 x.y.z 的记录器可以被视为名为 z 的记录器,它是 y 的子级,而 y 本身又是 x 的子级。默认情况下,当记录器收到日志时,它会将它们沿层次结构传播到其所有父级。因此,名为 'spam_application.auxiliary' 的记录器将其日志发送到 spam_application 记录器。如果您将名称更改为不再是该名称的子名称的名称,则日志将不会发送到那里。然而,所有记录器都是根记录器的子记录器,但这对您没有帮助,因为它的默认级别是 WARNING 并且高于 INFO

如果您的模块也按层次结构组织,则 __name__ 约定很有用,因为在这种情况下,记录器层次结构将与模块和子模块的层次结构完全匹配。

该文档还有一个有用的图表来帮助确定何时记录日志:https://docs.python.org/3/howto/logging.html#logging-flow

关于python - 日志记录在 python 3.9.4 中似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67593606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com