1.通過logging.basicConfig函數(shù)對日志的輸出格式及方式做相關(guān)配置
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)富陽,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
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.通過時(shí)間對日志進(jìn)行切割
import logging from logging.handlers import TimedRotatingFileHandler #創(chuàng)建logger,應(yīng)用程序日志輸入的接口 logger1=logging.getLogger() logger1.setLevel(logging.INFO) #設(shè)置logger日志級別,允許輸入的日志級別 #創(chuàng)建handler,日志輸出的接口 handler1 = logging.handlers.TimedRotatingFileHandler("test.log", 'M', 1, 0) #定義一個1分鐘換一次log文件的handler handler1.suffix = "%Y%m%d-%H%M.log" #設(shè)置日志文件的后綴,跟strftime的格式一樣 handler1.setLevel(logging.INFO) #設(shè)置handler日志級別,輸出的日志級別 #創(chuàng)建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介紹,可以將日志按照時(shí)間切分 #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的時(shí)間后,Logger會自動重建文件,當(dāng)然,這個文件的創(chuàng)建 # 取決于filename+suffix,若這個文件跟之前的文件有重名,則會自動覆蓋掉以前的文件,所以 # 有些情況suffix要定義的不能因?yàn)閣hen而重復(fù)。 # backupCount 是保留日志個數(shù)。默認(rèn)的0是不會自動刪除掉日志。若設(shè)10,則在文件的創(chuàng)建過程中 # 庫會判斷是否有超過這個10,若超過,則會從最先創(chuàng)建的開始刪除。
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 #是否繼承父節(jié)點(diǎn),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] #打印日志到文件,并按照設(shè)置時(shí)間對日志進(jìn)行切割 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).調(diào)用配置文件,打印日志
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')
本文題目:python-logging模塊使用介紹
標(biāo)題網(wǎng)址:http://bm7419.com/article8/geijip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、App開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、動態(tài)網(wǎng)站、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)