gpt4 book ai didi

Python。为类实例启用日志记录,从单独的模块导入

转载 作者:行者123 更新时间:2023-11-28 17:30:33 26 4
gpt4 key购买 nike

美好的一天。我正在尝试解决在 Python 中登录的问题。我正在使用 Python 3.5.1。我有一个应用程序,它使用一个从其他模块导入的类。我无法为其启用日志记录。这是一个简单的表示:

# test.py
import logging

from test_class import TestClass


logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.FileHandler('test_log.log', mode='w'))


if __name__ == '__main__':
logger.info('Importing class')
t = TestClass()
t.make_call()
t.make_another_call()
logger.info('End')

# test_class.py
import logging


class TestClass(object):

def __init__(self):
self.logger = logging.getLogger('test_class.TestClass')

def make_call(self):
self.logger.info('Make a call')

def make_another_call(self):
self.logger.info('Make another call')

如您所见,记录器必须将行写入文件(两个来自主模块,两个来自类。但是当我打开日志文件时,我看到:

# test_log.log
Importing class
End

因此,类中的两个记录器调用没有效果。任何想法,为什么它不起作用?提前谢谢你。

最佳答案

来自 the docs :

Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.

The name is potentially a period-separated hierarchical value, like foo.bar.baz (though it could also be just plain foo, for example). Loggers that are further down in the hierarchical list are children of loggers higher up in the list. For example, given a logger with a name of foo, loggers with names of foo.bar, foo.bar.baz, and foo.bam are all descendants of foo. The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(__name__). That’s because in a module, __name__ is the module’s name in the Python package namespace.

您在代码中调用 getLogger 的方式,test.py 中的调用是通过 __main__ 完成的,而 中的调用test_class.pytest_class 完成,因此后者不是前者的后代。

相反,如果在设置处理程序时对通过调用 getLogger() 不带参数获得的对象执行此操作,则您将在根日志记录对象上设置处理程序,并且对 getLogger() 的所有其他调用将在层次结构中进一步下降,并将使用您指定的处理程序。

如果您想继续在主模块中为您的日志语句设置名称,您可以在设置处理程序后再次调用 getLogger

例如:

# Call getLogger with no args to set up the handler
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.FileHandler('test_log.log', mode='w'))


if __name__ == '__main__':
# call getLogger again with a name to tag subsequent log statements
logger = logging.getLogger(__name__)
logger.info('Importing class')
t = TestClass()
t.make_call()
t.make_another_call()
logger.info('End')

关于Python。为类实例启用日志记录,从单独的模块导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683187/

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