logging模块
logging模块用于输出运行日志,可以设置不同的日志等级,保存信息到日志文件中等。 相比print,logging可以设置日志的等级,控制在发布版本中的输出内容,并且可以指定日志的输出格式。
1. 使用logging在终端输出日志
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging # 引入logging模块 # 设置打印日志级别 CRITICAL > ERROR > WARNING > INFO > DEBUG logging.basicConfig(level = logging.DEBUG,format = '%(asctime)s - %(name)s -%(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s') # 将信息打印到控制台上 logging.debug(u"调试") logging.info(u"执行打印功能") logging.warning(u"警告") logging.error(u"错误") logging.critical(u"致命错误")
输出
2. 使用logging在終端輸出日志,並保存日志到本地log文件
#!/usr/bin/env python # -*- coding:utf-8 -*- import logging # 引入logging模块 import os.path # 第一步,创建一个logger logger = logging.getLogger() logger.setLevel(logging.DEBUG) # Log等级开关 # 第二步,创建一个handler,用于写入日志文件 log_path = os.path.dirname(os.getcwd()) + '/Logs/' log_name = log_path + 'log.log' logfile = log_name file_handler = logging.FileHandler(logfile, mode='a+') file_handler.setLevel(logging.ERROR) # 输出到file的log等级的开关 # 第三步,定义handler的输出格式 formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s") file_handler.setFormatter(formatter) # 第四步,将handler添加到logger里面 logger.addHandler(file_handler) # 如果需要同時需要在終端上輸出,定義一個streamHandler print_handler = logging.StreamHandler() # 往屏幕上输出 print_handler.setFormatter(formatter) # 设置屏幕上显示的格式 logger.addHandler(print_handler) # 日志信息 logger.debug('this is a logger debug message') logger.info('this is a logger info message') logger.warning('this is a logger warning message') logger.error('this is a logger error message') logger.critical('this is a logger critical message') # 或使用logging logging.debug('this is a logger debug message') logging.info('this is a logger info message') logging.warning('this is a logger warning message') logging.error('this is a logger error message') logging.critical('this is a logger critical message')
日志等级划分
- FATAL:致命错误
- CRITICAL:特别糟糕的事情,如内存耗尽、磁盘空间为空,一般很少使用
- ERROR:发生错误时,如IO操作失败或者连接问题
- WARNING:发生很重要的事件,但是并不是错误时,如用户登录密码错误
- INFO:处理请求或者状态变化等日常事务
- DEBUG:调试过程中使用DEBUG等级,如算法中每个循环的中间状态
traceback模块
traceback是python中用来跟踪异常信息的模块,方便把程序中的运行异常打印或者保存下来做异常分析。
常见用法
try: doSomething() except: traceback.print_exc() # logging.error(str(traceback.format_exc()))
traceback.format_exc() 与 traceback.print_exc() 区别:
- traceback.format_exc() 返回异常信息的字符串,可以用来把信息记录到log里;
- traceback.print_exc() 直接把异常信息在终端打印出来;
traceback.print_exc()
也可以实现把异常信息写入文件,使用方法:
traceback.print_exc(file=open('traceback_INFO.txt','w+'))
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 《演灭Evotinction》正式登陆多平台发售!带你进入科幻潜行新境界
- 命运圣契2024公测最新兑换码大全 命运圣契开服可用兑换码汇总
- 银霞.1979-《回答·兰花草》新加坡限量复黑版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 不止黑神话!杭州给原创企业真金白银扶持 打造“杭州游戏”品牌
- 很有多样性:《战锤》首次引入非二元性别角色
- 《少女前线2:追放》项目组将裁员 官方:进一步优化组织结构
- 陈慧琳.2002-闪亮每一天新歌+精选2CD【正东】【WAV+CUE】
- 袁培华.1992-有缘【歌林】【WAV+CUE】
- 王瑞霞.1996-是我太软心【名冠】【WAV+CUE】
- 《谭维维 30首经典流行歌曲精选辑 2CD》[WAV/分轨][1.1GB]
- 《赵传 出道三十年全新大碟 你过得还好吗》[WAV/分轨][460MB]
- 《发烧四大天王 磁性男声HiFi发烧碟 2CD》[WAV/分轨][1GB]
- 黑神话悟空上品鼠司空精魄获取方法一览|上品鼠司空精魄收集攻略