【发布时间】:2021-06-03 00:52:18
【问题描述】:
我正在使用 python3。我想在一行中打印脚本失败的时间,以及相应的异常。参考this link & this one,我尝试了类似的方法,但该函数没有执行。我在这里做错了吗?非常感谢您的建议。
import atexit
import datetime
#define function to log when script ends due to error/failure
def f_log(date_and_time, error):
try:
print("")
except:
print('Script ended at date/time: %s with error %s.' % (date_and_time, error))
import atexit
atexit.register(f_log, date_and_time=datetime.datetime.now(), error=Exception)
print "Deliberate error" # Simple example of an error that should be caught & printed along with time when script fails.
# main body of script continues here
#
#
【问题讨论】:
标签: python exception logging error-handling