这应该是一个非常简单的问题,但在谷歌搜索,阅读文档和其他几个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")更多推荐
发布评论