Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3生产:不使用回溯模块记录异常_Python_Python 3.x_Logging_Production - Fatal编程技术网

Python3生产:不使用回溯模块记录异常

Python3生产:不使用回溯模块记录异常,python,python-3.x,logging,production,Python,Python 3.x,Logging,Production,在生产代码中,我们记录如下错误: error = {'tos': str(sys.exc_info()[0:2])} 但它只允许查看有关错误的此类信息: "tos": "(<class 'AttributeError'>, AttributeError(\"'NoneType' object has no attribute 'group'\",))" 但我们不在生产中使用回溯模块,因为它被认为太重了。因此,如何在不使用回溯的情况下获取行号和文件名?返回3个元素的元组,其中第三个

在生产代码中,我们记录如下错误:

error = {'tos': str(sys.exc_info()[0:2])}
但它只允许查看有关错误的此类信息:

"tos": "(<class 'AttributeError'>, AttributeError(\"'NoneType' object has no attribute 'group'\",))"
但我们不在生产中使用
回溯
模块,因为它被认为太重了。因此,如何在不使用回溯的情况下获取行号和文件名?

返回3个元素的元组,其中第三个是回溯

返回的元组类似于-
(类型、值、回溯)

您正在执行-
str(sys.exc_info()[0:2])
,它只选择前两个元素

试一试-

如果无法使用回溯模块格式化回溯。如果只需要异常的行号和文件名,可以使用以下命令-

sys.exc_info()[2].tb_frame.f_code.co_filename #<---- filename
sys.exc_info()[2].tb_lineno # <------ line number

sys.exc_info()[2].tb_frame.f_code.co_filename#我已经尝试过了,它只添加了请检查最新的答案。是的,我确实得到了行号和文件名,但它告诉我的不多-我确实需要跟踪到异常之前的最后一次函数调用。有没有办法做到这一点,或者唯一的办法是通过回溯模块?我相信只有通过回溯模块。否则它可能会变得非常复杂。
str(sys.exc_info())
sys.exc_info()[2].tb_frame.f_code.co_filename #<---- filename
sys.exc_info()[2].tb_lineno # <------ line number