写日志: #_author:'ZYB' #data:2020/5/23 import nnlog import traceback log = nnlog.Logger('python.log',level='debug',backCount=5,when='D') log.debug('eeee') #比较详细的信息 log.info('eeee') log.warning('wwww') log.error('wewewe') log.surprise() log.warning(traceback.format_exc()) #返回出错的那一大坨红色字符串信息 traceback.print_exc() #只print错误信息,无法通过log.的方法返回信息
异常处理: #_author:'ZYB' #data:2020/5/23 # l = [1,2,3] # d = {} # # try: # print(l[4]) # print(d['name']) # except IndexError as e: # print('下标越界') # print(e) # except KeyError as e: # print('key 不存在') # print(e) # except Exception as e: #这个Exception可以处理所有异常,上面两种是划分比较细的 # print('出现异常') # print(e) # else: #无异常的时候会执行else的内容 # print('all ok') # finally: #有无异常都会执行 # print('ok')