python-logging模块使用介绍-创新互联

1.通过logging.basicConfig函数对日志的输出格式及方式做相关配置

10年积累的成都网站设计、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有永仁免费网站建设让你可以放心的选择与我们合作。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 message Thu, 14 Mar 2019 09:56:00 test111.py[line:12] WARNING This is warning message

2.通过时间对日志进行切割

import logging from 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文件的handler handler1.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添加handler handler1.setFormatter(formatter1)         #给handler1添加formatter logging.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=DEBUG handlers=hand01,hand02 [logger_example01] handlers=hand01,hand02 qualname=example01 propagate=0                    #是否继承父节点,0为否,1为是。 [logger_example02] handlers=hand01,hand03 qualname=example02 propagate=0 ############################################### [handlers] keys=hand01,hand02,hand03 [handler_hand01]                #打印日志到屏幕 class=StreamHandler level=INFO formatter=form02 args=(sys.stderr,) [handler_hand02]                #打印日志到文件 class=FileHandler level=DEBUG formatter=form01 args=('myapp.log', 'a') [handler_hand03]                #打印日志到文件,并按照设置时间对日志进行切割 class=handlers.TimedRotatingFileHandler level=INFO formatter=form01 args=("test.log", 'S', 1, 0) ############################################### [formatters] keys=form01,form02 [formatter_form01] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s datefmt=%a, %d %b %Y %H:%M:%S [formatter_form02] format=%(name)-12s: %(levelname)-8s %(message)s datefmt=

(2).调用配置文件,打印日志

import logging import logging.config logging.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')

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:python-logging模块使用介绍-创新互联
文章网址:http://pwwzsj.com/article/edcdh.html