1. 想将日志保存在文件中
2. 想将文件既输出终端又输出到文件
3. 想输出各种程序输出
4. 想规范自己的日志格式
5. 想输出某个等级以上日志信息时
import logging
logging.debug('debug info')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
日志等级 critical > error > warning > info > debug > noset
import logging
#logging基础设置
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
python 的日志logging模块学习 python 简单封装