python-logging模块使用介绍
发表于:2024-11-29 作者:热门IT资讯网编辑
编辑最后更新 2024年11月29日,1.通过logging.basicConfig函数对日志的输出格式及方式做相关配置logging.basicConfig(level=logging.INFO,
1.通过logging.basicConfig函数对日志的输出格式及方式做相关配置
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='example.log', filemode='a')logging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')example.log文件:Thu, 14 Mar 2019 09:56:00 test111.py[line:11] INFO This is info messageThu, 14 Mar 2019 09:56:00 test111.py[line:12] WARNING This is warning message
2.通过时间对日志进行切割
import loggingfrom logging.handlers import TimedRotatingFileHandler#创建logger,应用程序日志输入的接口logger1=logging.getLogger()logger1.setLevel(logging.INFO) #设置logger日志级别,允许输入的日志级别#创建handler,日志输出的接口handler1 = logging.handlers.TimedRotatingFileHandler("test.log", 'M', 1, 0) #定义一个1分钟换一次log文件的handlerhandler1.suffix = "%Y%m%d-%H%M.log" #设置日志文件的后缀,跟strftime的格式一样handler1.setLevel(logging.INFO) #设置handler日志级别,输出的日志级别#创建formatter,日志格式formatter1 = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger1.addHandler(handler1) #给logger1添加handlerhandler1.setFormatter(formatter1) #给handler1添加formatterlogging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')#TimedRotatingFileHandler介绍,可以将日志按照时间切分#TimedRotatingFileHandler(filename [,when [,interval [,backupCount]]])# filename 是输出日志文件名的前缀# when 是一个字符串的定义如下:# "S": Seconds# "M": Minutes# "H": Hours# "D": Days# "W": Week day (0=Monday)# "midnight": Roll over at midnight# interval 是指等待多少个单位when的时间后,Logger会自动重建文件,当然,这个文件的创建# 取决于filename+suffix,若这个文件跟之前的文件有重名,则会自动覆盖掉以前的文件,所以# 有些情况suffix要定义的不能因为when而重复。# backupCount 是保留日志个数。默认的0是不会自动删除掉日志。若设10,则在文件的创建过程中# 库会判断是否有超过这个10,若超过,则会从最先创建的开始删除。
3.通过配置文件方式配置日志
(1).logger.conf文件
#logger.conf###############################################[loggers]keys=root,example01,example02[logger_root]level=DEBUGhandlers=hand01,hand02[logger_example01]handlers=hand01,hand02qualname=example01propagate=0 #是否继承父节点,0为否,1为是。[logger_example02]handlers=hand01,hand03qualname=example02propagate=0###############################################[handlers]keys=hand01,hand02,hand03[handler_hand01] #打印日志到屏幕class=StreamHandlerlevel=INFOformatter=form02args=(sys.stderr,)[handler_hand02] #打印日志到文件class=FileHandlerlevel=DEBUGformatter=form01args=('myapp.log', 'a')[handler_hand03] #打印日志到文件,并按照设置时间对日志进行切割class=handlers.TimedRotatingFileHandlerlevel=INFOformatter=form01args=("test.log", 'S', 1, 0)###############################################[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)sdatefmt=%a, %d %b %Y %H:%M:%S[formatter_form02]format=%(name)-12s: %(levelname)-8s %(message)sdatefmt=
(2).调用配置文件,打印日志
import loggingimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example01")logger.debug('This is debug message')logger.info('This is info message')logger.warning('This is warning message')# import logging# import logging.config## logging.config.fileConfig("logger.conf")# logger = logging.getLogger("example02")## logger.debug('This is debug message')# logger.info('This is info message')# logger.warning('This is warning message')