这个函数应该在主执行过程中捕获异常。如果有异常,它应该用log.error(traceback.print_exc())
打印错误,用exit_main()
清理。
def main():
try:
exec_app()
except KeyboardInterrupt:
log.error('Error: Backup aborted by user.')
exit_main()
except Exception:
log.error('Error: An Exception was thrown.')
log.error("-" * 60)
log.error(traceback.print_exc())
log.error("-" * 60)
exit_main()
不幸的是,log.error(traceback.print_exc())
只在出现异常时才返回None
。在这种情况下,如何使跟踪打印完整的错误报告?
PS:我使用python3.4。
发布于 2016-10-25 12:41:36
从它的__doc__
Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'
也就是说,它不应该返回任何东西,它的工作是打印。如果希望将跟踪作为字符串记录,请使用traceback.format_exc()
。
发布于 2016-10-25 13:09:18
我通常只使用traceback.print_exc()
进行调试。在这种情况下,要记录异常,只需执行以下操作:
try:
# Your code that might raise exceptions
except SomeSpecificException as e:
# Do something (log the exception, rollback, etc)
except Exception as e:
log.error(e) # or log(e.message) if you want to log only the message and not all the error stack
https://stackoverflow.com/questions/40240206
复制相似问题