Python:sys.excepthook和日志记录跨多个模块的未捕获的异常 [英] Python: sys.excepthook and logging uncaught exceptions across multiple modules

查看:843
本文介绍了Python:sys.excepthook和日志记录跨多个模块的未捕获的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我所有的Python主要脚本和模块中,我一直在尝试实现一种将未捕获的异常记录到抛出异常记录器的模块中。我在所有的文件中都这样做:

In all of my Python main scripts and modules, I have been trying to implement a way to log uncaught exceptions to the module in which the exception was thrown's logger. I'm doing this the same way in all of my files:

def log_unhandled_exception(*exc_info):
   text = "".join(traceback.format_exception(*exc_info))
   logger.critical("An unhandled exception has caused this script to terminate prematurely.  Here are the details: {0}".format(text))
   sys.exit(2)

def some_function():
   # ... 

sys.excepthook = log_unhandled_exception
logger = logging.getLogger("Module1") # or "Module2", "Module3", etc., each module has it's own logger

当我遇到一个未捕获的异常时,我有时不会得到预期的记录器。我认为它与我导入模块的排序有关:如果我导入module1然后导入module2,然后调用module2中的函数并运行到一个异常,似乎我得到module1的记录器。相反,如果我反转导入顺序(module2来自module1),我尝试相同的测试(在module2中抛出异常),我正确地获取了module2的记录器。我会以为LATER导入优先级(覆盖前者的sys.excepthook参考),但不是。

When I run into an uncaught exception, I sometimes don't get the intended logger. I think it has to do with the ordering that I import the modules: if I import module1 then import module2, and then call a function in module2 and run into an exception, it seems I get module1's logger. Conversely if I reverse the order of imports (module2 comes before module1), and I attempt the same test (exception thrown in module2), I correctly get module2's logger. I would have thought the LATER import would take precedence (overwrite the former's sys.excepthook reference), but no.

我能够解决这个问题(我猜.. 。)通过在每个模块中给每个记录器引用一个唯一的名称。所以要修改上面的代码,这个模式可以不考虑模块导入的顺序:

I was able to solve this problem (I guess...) by giving each logger reference a unique name in each module. So to amend the code above, THIS pattern works without regard to the order of module imports:

def log_unhandled_exception(*exc_info):
   text = "".join(traceback.format_exception(*exc_info))
   module1_logger.critical("An unhandled exception has caused this script to terminate prematurely.  Here are the details: {0}".format(text))
   sys.exit(2)

def some_function():
   # ... 

sys.excepthook = log_unhandled_exception
module1_logger = logging.getLogger("Module1") # or "Module2", "Module3", etc., each module has it's own logger

这是实现我想要的目标的正确方法,这是每个模块都有自己的记录器,每个模块都使用它的记录器记录未捕获的异常。

Is this the proper way to achieve my desired goal, which is for every module to have it's own logger, and every module to use it's logger to log uncaught exceptions.

(实际代码更多样化,并且每个模块都有自己的实现log_unhandled_exception()的原因)

(The actual code is more varied, and has a reason for every module to have their own implementation of log_unhandled_exception())

推荐答案

sys.excepthook 是全球对你的Python程序。你设置的最后一个值赢了。在模块记录器的不同文件中使用唯一的名称不会对其造成任何影响。

sys.excepthook is global for your Python process. The last value that you set to it wins. Using unique names in different files for module loggers won't have any effect on it.

这篇关于Python:sys.excepthook和日志记录跨多个模块的未捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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