logging.basicConfig()
的参数:
level
:输出日志的等级,debug<info<warning<critical<error
format
:输出格式
datemat
:时间信息
filename
:日志信息输出到的日志文件名
filemode
:决定使用什么模式来打开日志文件('r'
、'w'
、'a'
),默认为'a'
import logging
'''format=%(asctime)s具体时间 %(filename)s文件名 %(lenvelname)s日志等级 %(message)s具体信息
datemt=%a星期 %d日期 %b月份 %Y年份 %H:%M:%S时间'''
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a %d %b %Y %H:%M:%S', filename='my.log', filemode='w')
logging.warning('this is warning')
logging.info('this is info')
logging.debug('this is debug')
logging.error('this is error')
logging.critical('this is critical')
输出结果是:
在my.log文件中:
Thu 16 Apr 2020 16:48:26 test_logging.py WARNING this is warning
Thu 16 Apr 2020 16:48:26 test_logging.py INFO this is info
Thu 16 Apr 2020 16:48:26 test_logging.py DEBUG this is debug
Thu 16 Apr 2020 16:48:26 test_logging.py ERROR this is error
Thu 16 Apr 2020 16:48:26 test_logging.py CRITICAL this is critical