Python logging模块
日志作用:
调试
辅助定位问题
数据分析
日志的级别

五种日志级别按从低到高排序:
DEBUG < INFO < WARNING < ERROR < CRITICAL
日志的用法:
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s -%(message)s')
logging.debug('Some debugging details.')
logging.info('The logging module is working')
logging.warning('An error message is about to be logged.')
logging.error('An error has occurred.')
logging.critical('The program is unable to recover!')
官网信息:
https://docs.python.org/3/howto/logging.html
basicConfig需要在最前面定义好
import logging
#默认设置warning级别,即warning级别以上log才会打印
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
设置默认级别
logging.basicConfig(level=logging.INFO)
设置log保存在文件里
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
显示日期和时间
import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')
更改日期时间显示格式
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')

浙公网安备 33010602011771号