Python中的日志模块
日志的作用
- 日记
- 程序行为
- 重要信息记录
日志的等级
debuginfowarningerrorcritical
logging模块的使用
logging.basicConfig
| 参数名 | 作用 | 举例 |
|---|---|---|
level |
日志输出等级 | level = logging.DEBUG |
format |
日志输出格式 | |
filename |
存储位置 | filename = 'd://back.log' |
filemode |
输入模式 | filemode = "w" |
format具体格式
| 格式符 | 含义 |
|---|---|
%(levelname)s |
日志级别名称 |
%(pathname)s |
执行程序的路径 |
%(filename)s |
执行程序名 |
%(lineno)d |
日志的当前行号 |
%(asctime)s |
打印日志的时间 |
%(message)s |
日志信息 |
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'- (常用的格式)
代码
| # coding:utf-8 | |
| import logging | |
| import os | |
| def init_log(path): | |
| if os.path.exists(path): | |
| mode = 'a' | |
| else: | |
| mode = 'w' | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s %(filename)s %(lineno)d %(levelname)s %(message)s', | |
| filename=path, | |
| filemode=mode | |
| ) | |
| return logging | |
| current_path = os.getcwd() | |
| path = os.path.join(current_path, 'back.log') | |
| log = init_log(path) | |
| # log = init_log() | |
| log.info('这是第一个记录的日志信息') | |
| log.warning('这是一个警告') | |
| log.error('这是一个重大的错误信息') | |
| log.debug('这是一个debug') |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ZkeqのCoding日志!
评论





