语法:Python中的logging模块
来源:互联网 发布:淘宝垃圾箱在哪 编辑:程序博客网 时间:2024/05/20 07:54
新年第一天,祝大家元旦快乐!偷个小懒,怒转一篇优质好文。
编程时为了方便追踪调试程序中的bug,我们通常借助于日志分析来快速定位程序出现的错误,python作为最活跃的一门高级编程语言(之一),自然有相应的logging module。
当然精确无误的使用logging只能去官网了HOWTOs文档了,如果不想看英文又想快速学习基础用法,那只能是这篇快速实战教程了(个人理解),再此谢过推酷Python Logging 模块。
直接上代码:
import loggingimport sys# logger=logging.getLogger(__name__)# logging.info("hello")import loggingimport sys# 获取logger实例,如果参数为空则返回root loggerlogger = logging.getLogger("__name__")# 指定logger输出格式formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')# 文件日志file_handler = logging.FileHandler("../log/mylogging"+".log")file_handler.setFormatter(formatter) # 可以通过setFormatter指定输出格式# 控制台日志console_handler = logging.StreamHandler(sys.stdout)console_handler.formatter = formatter # 也可以直接给formatter赋值# 为logger添加的日志处理器logger.addHandler(file_handler)logger.addHandler(console_handler)# 指定日志的最低输出级别,默认为WARN级别logger.setLevel(logging.INFO)# 输出不同级别的loglogger.debug('this is debug info')logger.info('this is information')logger.warn('this is warning message')logger.error('this is error message')logger.fatal('this is fatal message, it is same as logger.critical')logger.critical('this is critical message')
控制台结果:
日志文件结果:
Python中的logging模块分为不同的级别,优先级从大到小依次为:
critical>error>warning>info>debug
他们使用条件为:
debug级别最低,列出最详尽的信息,通常在诊断问题时才会选择这个级别;info证实程序按照预期发展时会用到;warning程序超出预期或是警示以后可能会出现的问题,出现该级别时程序依旧可以运行;error说明程序出现了严重性的错误,造成某些功能不能运行;critical级别最高,通常是严重阻碍了程序的正常运行。
0 0
- 语法:Python中的logging模块
- python中的logging模块
- Python中的logging模块
- Python中的logging模块
- 【python】Python中的logging模块
- python中的日志logging模块
- Python中的logging模块就这么用
- Python中的logging模块就这么用
- Python中的logging模块就这么用
- python模块 ---logging模块
- python logging模块学习
- Python标准模块logging
- Python标准模块logging
- python logging模块
- python 之logging模块
- python的logging模块
- Python标准模块logging
- [python相关]logging模块
- NOIP2009 提高组 复赛 spy 潜伏者
- 文件格式类型
- Centos7 安装mysql
- No mapping found for HTTP request with URI [/SuperPowerfulProject/login.do] in DispatcherServlet wit
- Saving James Bond - Easy Version
- 语法:Python中的logging模块
- MongoDB 客户端工具
- matlab像素值及统计
- CentOS 安装rz和sz命令 结合xshell
- [bzoj 1011] [HNOI2008]遥远的行星:近似算法(一种正确性显然的非乱搞的科学做法)
- ubuntu虚拟机下使用minicom读取串口数据
- 论一个女程序员的自我修养
- 学习篇--快速查询未知长度单链表的中间元素
- Centos7 JDK8安装配置