1
我有一個功能,把except
部分放在try
裏,我希望它記錄錯誤信息。問題是我無法得到產生錯誤的代碼行,因爲我以前可以這樣做。看看這段代碼:linecache.getline()什麼也沒有返回
def func():
try:
a = 1
b = 0
print a/b
except:
Debug.log()
這是日誌功能:
def log(cls):
try:
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
line_no = str(tb.tb_lineno)
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, line_no, f.f_globals)
current_frame = inspect.currentframe()
previous_frame = current_frame.f_back
func_name = previous_frame.f_code.co_name
msg = str(exc_obj)
obj = Error()
obj.filename = filename
obj.line_no = line_no
obj.line = line
obj.msg = msg
obj.function = func_name
obj.save()
執行後,我可以看到行是空:
{
"_id" : ObjectId("5413d67a65765f16219aa575"),
"filename" : "E:/Developer Center/DomainServices/DomainServices/test.py",
"line_no" : "23",
"line" : "",
"msg" : "integer division or modulo by zero",
"function" : "func",
}
有什麼不對?