Python常用模块logging——日志输出
2020-01-13 20:07
387 查看
用途
logging模块是Python的内置模块,主要用于输出运行日志,可以灵活配置输出日志的各项信息。
基本使用方法
logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a")
参数
日志一共分成5个等级,从低到高分别是:DEBUG ,INFO ,WARNING ,ERROR, CRITICAL。
%(levelno)s: 打印日志级别的数值 %(levelname)s: 打印日志级别名称 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0] %(filename)s: 打印当前执行程序名 %(funcName)s: 打印日志的当前函数 %(lineno)d: 打印日志的当前行号 %(asctime)s: 打印日志的时间 %(thread)d: 打印线程ID %(threadName)s: 打印线程名称 %(process)d: 打印进程ID %(message)s: 打印日志信息
调用
logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message')
示例
import logging logging.basicConfig(level=logging.DEBUG, format='levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='./loggmsg.log', filemode="a") logging.debug("Hello")
日志文件loggmsg.log
levelname:DEBUG filename: test.py outputNumber: [7] thread: MainThread output msg: Hello - [19/Nov/2019 22:28:13]
想进一步了解编程开发相关知识,与我一同成长进步,请关注我的公众号“松果仓库”,共同分享宅&程序员的各类资源,谢谢!!!
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Python常用模块logging――日志输出功能(示例代码)
- Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)
- Python模块:日志输出—logging模块
- Python同时向控制台和文件输出日志logging的方法 Python logging模块详解
- Python Logging模块 输出日志颜色、过期清理和日志滚动备份
- Python日志输出——logging模块
- Python logging模块无法正常输出日志
- Python日志输出——logging模块
- Python3 使用 logging 模块输出日志中的中文乱码解决办法
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python日志输出——logging模块
- Python +Selenium logging模块打印日志并输出到记事本中
- python 的日志logging模块学习
- python logging动态变更输出日志文件名
- python-logging.config【日志模块】
- Python模块学习:logging 日志记录
- Python接口自动化——logging日志模块