未来的简单Python日志异常(Simple Python Logging Exception from Future)

编程入门 行业动态 更新时间:2024-10-10 17:31:44
未来的简单Python日志异常(Simple Python Logging Exception from Future)

这应该是一个非常简单的问题,但在谷歌搜索,阅读文档和其他几个SO线程后,我没有看到答案:如何使用Python标准日志记录异常? 一个小皱纹是我从未来获得例外。 我自己不是在写异常处理程序。 理想情况下,我会得到异常消息,堆栈跟踪,发送的额外消息,以及可能的异常类型。 这是一个显示我的问题的简单程序:

import logging from concurrent.futures import ThreadPoolExecutor logger = logging.getLogger(__name__) def test_f(a, b=-99, c=50): logger.info("test_f a={} b={} c={}".format(a, b, c)) def future_callback_error_logger(future): e = future.exception() if e is not None: # This log statement does not seem to do what I want. # It logs "Executor Exception" with no information about the exception. # I would like to see the exception type, message, and stack trace. logger.error("Executor Exception", exc_info=e) def submit_with_log_on_error(executor, func, *args, **kwargs): future = executor.submit(func, *args, **kwargs) future.add_done_callback(future_callback_error_logger) if __name__ == "__main__": logging.basicConfig(level="DEBUG") logger.info("start") executor = ThreadPoolExecutor(max_workers=5) # This will work. submit_with_log_on_error(executor, test_f, 10, c=20) # This will intentionally trigger an error due to too many arguments. # I would like that error to be properly logged. submit_with_log_on_error(executor, test_f, 10, 20, 30, 40) # This will work. submit_with_log_on_error(executor, test_f, 50, c=60) executor.shutdown(True) logger.info("shutdown")

This should be a really simple question, but after googling, reading docs, and several other SO threads, I don't see the answer: How do I log an exception with Python standard logging? One small wrinkle is that I'm getting the exception from a Future. I'm not writing the except exception handler myself. Ideally, I would get the exception message, a stack trace, the extra message sent, and maybe the type of exception. Here's a simple program that shows my issue:

import logging from concurrent.futures import ThreadPoolExecutor logger = logging.getLogger(__name__) def test_f(a, b=-99, c=50): logger.info("test_f a={} b={} c={}".format(a, b, c)) def future_callback_error_logger(future): e = future.exception() if e is not None: # This log statement does not seem to do what I want. # It logs "Executor Exception" with no information about the exception. # I would like to see the exception type, message, and stack trace. logger.error("Executor Exception", exc_info=e) def submit_with_log_on_error(executor, func, *args, **kwargs): future = executor.submit(func, *args, **kwargs) future.add_done_callback(future_callback_error_logger) if __name__ == "__main__": logging.basicConfig(level="DEBUG") logger.info("start") executor = ThreadPoolExecutor(max_workers=5) # This will work. submit_with_log_on_error(executor, test_f, 10, c=20) # This will intentionally trigger an error due to too many arguments. # I would like that error to be properly logged. submit_with_log_on_error(executor, test_f, 10, 20, 30, 40) # This will work. submit_with_log_on_error(executor, test_f, 50, c=60) executor.shutdown(True) logger.info("shutdown")

最满意答案

要使用logger.exception并获取回溯等,您需要在except块内。 而不是检查返回异常(如果有)的future.result() , future.result()使用future.result()来引发异常(如果有的话)。

所以, try这个(没有双关语):

def future_callback_error_logger(future): try: future.result() except Exception: logger.exception("Executor Exception")

To use logger.exception and get the traceback etc, you need to be inside an except block. Instead of checking the future.exception(), which returns the exception (if any), use the future.result() which raises the exception (if any).

So, try this instead (no pun intended):

def future_callback_error_logger(future): try: future.result() except Exception: logger.exception("Executor Exception")

更多推荐

本文发布于:2023-04-29 08:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   未来   简单   日志   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!