logging模塊
簡介
logging模塊是python內(nèi)置模塊,專門為打印日志的模塊
import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
默認(rèn)情況下python的logging模塊將日志打印到標(biāo)準(zhǔn)輸出中,且只顯示了大于等于WARNING的日志,這說明默認(rèn)的日志級別為WARNING(日志級別等級CRITICAL > ERROR > WARN > INFO > DEBUG),默認(rèn)的日志格式日志級別:Logger名稱:用戶輸出消息
靈活配置日志級別,日志格式,輸出位置 :
import logging
fh = logging.FileHandler(filename='xxx.log',encoding='utf-8') # 存放日志的文件
fh1 = logging.FileHandler(filename='xxx2.log',encoding='utf-8')
sh = logging.StreamHandler() # 屏幕打印日志
logging.basicConfig(level=logging.INFO, # 日志的級別
handlers=[fh,sh,fh1],
datefmt='%Y-%m-%d %H:%M:%S', # 時間
format='%(asctime)s - %(name)s[%(lineno)d] - % (levelname)s -%(module)s: %(message)s') # 格式化格式輸出
logging.debug('debug message') # 情況越輕
logging.info('info message') # 信息類的日志
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
日志切割
import time
import logging
from logging import handlers
sh = logging.StreamHandler()
# 按文件大寫進(jìn)行切割,只保留5個文件
rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024,backupCount=5)
# 按時間進(jìn)行切割
fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5, encoding='utf-8')
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[fh,sh,rh],
level=logging.ERROR
)
for i in range(1,100000):
time.sleep(1)
logging.error('KeyboardInterrupt error %s'%str(i))
配置參數(shù):
logging.basicConfig() # 函數(shù)中可通過具體參數(shù)來更改logging模塊默認(rèn)行為,可用參數(shù)有: filename:# 用指定的文件名創(chuàng)建FiledHandler,這樣日志會被存儲在指定的文件中。 filemode:# 文件打開方式,在指定了filename時使用這個參數(shù),默認(rèn)值為“a”還可指定為“w”。 format:# 指定handler使用的日志顯示格式。 datefmt:# 指定日期時間格式。 level:# 設(shè)置rootlogger(后邊會講解具體概念)的日志級別 stream:# 用指定的stream創(chuàng)建StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默認(rèn)為sys.stderr。若同時列出了filename和stream兩個參數(shù),則stream參數(shù)會被忽略。 #format參數(shù)中可能用到的格式化串: %(name)s #Logger的名字 %(levelno)s #數(shù)字形式的日志級別 %(levelname)s #文本形式的日志級別 %(pathname)s #調(diào)用日志輸出函數(shù)的模塊的完整路徑名,可能沒有 %(filename)s #調(diào)用日志輸出函數(shù)的模塊的文件名 %(module)s #調(diào)用日志輸出函數(shù)的模塊名 %(funcName)s #調(diào)用日志輸出函數(shù)的函數(shù)名 %(lineno)d #調(diào)用日志輸出函數(shù)的語句所在的代碼行 %(created)f #當(dāng)前時間,用UNIX標(biāo)準(zhǔn)的表示時間的浮 點(diǎn)數(shù)表示 %(relativeCreated)d #輸出日志信息時的,自Logger創(chuàng)建以 來的毫秒數(shù) %(asctime)s #字符串形式的當(dāng)前時間。默認(rèn)格式是 “2003-07-08 16:49:45,896”。逗號后面的是毫秒 %(thread)d #線程ID。可能沒有 %(threadName)s #線程名。可能沒有 %(process)d #進(jìn)程ID。可能沒有 %(message)s #用戶輸出的消息
logger對象配置
import logging
logger = logging.getLogger()
# 創(chuàng)建一個handler,用于寫入日志文件
fh = logging.FileHandler('test.log',encoding='utf-8')
# 再創(chuàng)建一個handler,用于輸出到控制臺
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh) #logger對象可以添加多個fh和ch對象
logger.addHandler(ch)
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')
logging庫提供了多個組件:Logger、Handler、Filter、Formatter。Logger對象提供應(yīng)用程序可直接使用的接口,Handler發(fā)送日志到適當(dāng)?shù)哪康牡兀現(xiàn)ilter提供了過濾日志信息的方法,F(xiàn)ormatter指定日志顯示格式。另外,可以通過:logger.setLevel(logging.Debug)設(shè)置級別,當(dāng)然,也可以通過
fh.setLevel(logging.Debug)單對文件流設(shè)置某個級別。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
