标签:超过 函数 包含 很多 import font ogg pat 存储
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug()调试, info()记录, warning()有潜在问题, error()出问题 and critical()严重问题5个级别,下面我们看一下怎么用。
最简单用法
import logging logging.warning("user [alex] attempted wrong password more than 3 times") #用户 alex 输错密码超过三次 logging.critical("server is down") #服务器坏了
输出
WARNING:root:user [alex] attempted wrong password more than 3 times CRITICAL:root:server is down
输出到文件
import logging logging.basicConfig(filename=‘log_test‘,level=logging.INFO) #只添加比level中级别更高的,多次添加会追加 logging.debug(‘The message should go to the log file.‘) logging.info(‘So shoule this‘) logging.warning(‘And this, too.‘)
自定义日志格式
import logging #加上格式、时间 logging.basicConfig(filename=‘log_test‘, level=logging.DEBUG, format=‘%(asctime)s %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘) logging.debug(‘The message should go to the log file.‘) logging.info(‘So shoule this‘) logging.warning(‘And this, too.‘)
除了加时间,还可以自定义一大堆格式,下表就是所有支持的格式
| %(name)s | Logger的名字 |
|---|---|
| %(levelno)s | 数字形式的日志级别 |
| %(levelname)s | 文本形式的日志级别 |
| %(pathname)s | 调用日志输出函数的模块的完整路径名,可能没有 |
| %(filename)s | 调用日志输出函数的模块的文件名 |
| %(module)s | 调用日志输出函数的模块名 |
| %(funcName)s | 调用日志输出函数的函数名 |
| %(lineno)d | 调用日志输出函数的语句所在的代码行 |
| %(created)f | 当前时间,用UNIX标准的表示时间的浮 点数表示 |
| %(relativeCreated)d | 输出日志信息时的,自Logger创建以 来的毫秒数 |
| %(asctime)s | 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 |
| %(thread)d | 线程ID。可能没有 |
| %(threadName)s | 线程名。可能没有 |
| %(process)d | 进程ID。可能没有 |
| %(message)s | 用户输出的消息 |
Python全栈之路----常用模块----logging模块
标签:超过 函数 包含 很多 import font ogg pat 存储
原文地址:https://www.cnblogs.com/Moxiaoyu666/p/10547063.html