Python是否可以通过使用同名的getLogger意外覆盖记录器? [英] Python can you accidentally overwrite a logger by using getLogger with the same name?

查看:143
本文介绍了Python是否可以通过使用同名的getLogger意外覆盖记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python程序中,我导入一个模块(假设它称为bananas).通过执行以下操作,模块将在其__init__.py中获取记录器: _logger = logging.getLogger(__name__).因此,记录器的名称为bananas.

In my Python program, I import a module (let's say it's called bananas). The module gets a logger inside its __init__.py by doing this: _logger = logging.getLogger(__name__). Thus, the logger's name is bananas.

在我的程序(导入模块bananas的程序)中,我也有这行:my_logger = logging.getLogger("bananas").

In my program (the one that imports the module bananas), I also have this line: my_logger = logging.getLogger("bananas").

这样做:

  • 是否覆盖bananas中的_logger?
  • 获取该记录器,因此my_logger现在等于_logger?
  • 完全其他的东西
  • Overwrite _logger made in bananas?
  • Fetch that logger, and thus my_logger now equals _logger?
  • Something else entirely

谢谢您的帮助!

推荐答案

它获取bananas记录器,以便my_loggerbananas._logger是同一对象.

It fetches the bananas logger, so that my_logger is the same object as bananas._logger.

这篇关于Python是否可以通过使用同名的getLogger意外覆盖记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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