默认级别:warning
import logging
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
输出:
WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message
调整logging级别:
logging.basicConfig(level=logging.级别)
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
调整到debug级别,所有的logging都会在console中输出
DEBUG:root:debug message
INFO:root:info message
WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message
输出到log日志文件
logging.basicConfig(filename='文件名.log', level=logging.级别)
import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')
日志输出到指定的日志文件 test.log,控制台中没有输出。内容与上一节相同。