0

我目前正在使用 python 日志记录模块来记录一些潜在的错误/异常。但是,我使用 log.debug 捕获并输出了一个异常,我不想要整个堆栈跟踪(它很大)。

我们已经审查了异常并且注意到它已经发生了,它不会影响程序的系统或流程。

        except sql_a.exc.IntegrityError:
            self.log.debug('insertion of non-unique row', df.iloc[[row]],
                    stack_info=False, exc_info=False)

关于上面的代码片段,我尝试过使用 exc_info=False 和/或 stack_info=False。但是,在异常被捕获后,我仍在输出整个堆栈跟踪。我已阅读有关 exc_info 的文档并假设我正确应用它?

如果 exc_info 未评估为 false,则会将异常信息添加到日志消息中。 https://docs.python.org/3/library/logging.html#logging.Logger.debug

知道我在这里做错了什么吗?我浏览了一些关于记录异常的stackoverflow帖子,但是我还没有真正找到我正在寻找的/解决我的问题,如果之前重复了这个问题,我深表歉意。

编辑:

handleError(record) 当在 emit() 调用期间遇到异常时,应该从处理程序调用此方法。如果模块级属性 raiseExceptions 为 False,则 > 异常会被静默忽略。 https://docs.python.org/3/library/logging.html#logging.Handler.handleError

我也尝试将属性 (handleError) 设置为 false,但我仍然得到完整的堆栈跟踪输出到 stdout/stderr。

    stream_handler.raiseExceptions = False
    handler.raiseExceptions = False
4

1 回答 1

0

我找到了这个错误的原因,因为我还没有找到类似的问题/答案,所以我将把它留在这里。(直到有人决定删除它,没有难过的感觉)

self.log.debug('insertion of non-unique row', df.iloc[[row]],
        stack_info=False, exc_info=False)

问题是因为我没有遵循logging.debug()模块接受的参数格式。

debug(msg, *args, **kwargs)

https://docs.python.org/3/library/logging.html#logging.Logger.debug

如果我正确格式化参数一切都很好,为了保持一致性,我将在下面留下我当前的工作示例:

self.log.debug('insertion of non-unique row:\n{}'.format(df.iloc[[row]]),
        stack_info=False, exc_info=False)
于 2021-03-05T05:36:52.887 回答