python logging模塊的使用
默認(rèn)情況下Python的logging模塊將日志打印到了標(biāo)準(zhǔn)輸出中,且只顯示了大于等于WARNING級(jí)別的日志,這說明默認(rèn)的日志級(jí)別設(shè)置為WARNING(日志級(jí)別等級(jí)CRITICAL > ERROR > WARNING > INFO > DEBUG),默認(rèn)的日志格式為日志級(jí)別:Logger名稱:用戶輸出消息。
靈活配置日志級(jí)別,日志格式,輸出位置
import loggingfile_handler = logging.FileHandler(filename=’x1.log’, mode=’a’, 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=[file_handler,], level=logging.ERROR)logging.error(’你好’)
日志切割
import timeimport loggingfrom logging import handlerssh = logging.StreamHandler()rh = handlers.RotatingFileHandler(’myapp.log’, maxBytes=1024,backupCount=5)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,這樣日志會(huì)被存儲(chǔ)在指定的文件中。filemode:文件打開方式,在指定了filename時(shí)使用這個(gè)參數(shù),默認(rèn)值為“a”還可指定為“w”。format:指定handler使用的日志顯示格式。datefmt:指定日期時(shí)間格式。level:設(shè)置rootlogger(后邊會(huì)講解具體概念)的日志級(jí)別stream:用指定的stream創(chuàng)建StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默認(rèn)為sys.stderr。若同時(shí)列出了filename和stream兩個(gè)參數(shù),則stream參數(shù)會(huì)被忽略。
format參數(shù)中可能用到的格式化串:%(name)s Logger的名字%(levelno)s 數(shù)字形式的日志級(jí)別%(levelname)s 文本形式的日志級(jí)別%(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)前時(shí)間,用UNIX標(biāo)準(zhǔn)的表示時(shí)間的浮 點(diǎn)數(shù)表示%(relativeCreated)d 輸出日志信息時(shí)的,自Logger創(chuàng)建以 來的毫秒數(shù)%(asctime)s 字符串形式的當(dāng)前時(shí)間。默認(rèn)格式是 “2003-07-08 16:49:45,896”。逗號(hào)后面的是毫秒%(thread)d 線程ID。可能沒有%(threadName)s 線程名。可能沒有%(process)d 進(jìn)程ID。可能沒有%(message)s用戶輸出的消息
屬性 格式 描述 asctime %(asctime)s 日志產(chǎn)生的時(shí)間,默認(rèn)格式為2003-07-08 16:49:45,896 created %(created)f time.time()生成的日志創(chuàng)建時(shí)間戳 filename %(filename)s 生成日志的程序名 funcName %(funcName)s 調(diào)用日志的函數(shù)名 levelname %(levelname)s 日志級(jí)別 (’DEBUG’, ’INFO’, ’WARNING’, ’ERROR’, ’CRITICAL’) levelno %(levelno)s 日志級(jí)別對應(yīng)的數(shù)值 lineno %(lineno)d 日志所針對的代碼行號(hào)(如果可用的話) module %(module)s 生成日志的模塊名 msecs %(msecs)d 日志生成時(shí)間的毫秒部分 message %(message)s 具體的日志信息 name %(name)s 日志調(diào)用者 pathname %(pathname)s 生成日志的文件的完整路徑 process %(process)d 生成日志的進(jìn)程ID(如果可用) processName %(processName)s 進(jìn)程名(如果可用) thread %(thread)d 生成日志的線程ID(如果可用) threadName %(threadName)s 線程名(如果可用)
logger對象配置
針對不同的數(shù)據(jù)流設(shè)置不同的日志級(jí)別。
import logginglogger = logging.getLogger()# 創(chuàng)建一個(gè)handler,用于寫入日志文件fh = logging.FileHandler(’test.log’,encoding=’utf-8’)fh.setLevel(logging.DEBUG)# 再創(chuàng)建一個(gè)handler,用于輸出到控制臺(tái) ch = logging.StreamHandler()ch.setLevel(logging.INFO)formatter = logging.Formatter(’%(asctime)s - %(name)s - %(levelname)s - %(message)s’)fh.setFormatter(formatter) ch.setFormatter(formatter) #logger對象可以添加多個(gè)fh和ch對象 logger.addHandler(fh) 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庫提供了多個(gè)組件:Logger、Handler、Filter、Formatter。Logger對象提供應(yīng)用程序可直接使用的接口,Handler發(fā)送日志到適當(dāng)?shù)哪康牡兀現(xiàn)ilter提供了過濾日志信息的方法,F(xiàn)ormatter指定日志顯示格式。另外,可以通過:logger.setLevel(logging.Debug)設(shè)置級(jí)別,當(dāng)然,也可以通過fh.setLevel(logging.Debug)單對文件流設(shè)置某個(gè)級(jí)別。
以上就是python logging模塊的使用的詳細(xì)內(nèi)容,更多關(guān)于python logging模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車3. ASP.Net Core對USB攝像頭進(jìn)行截圖4. .net如何優(yōu)雅的使用EFCore實(shí)例詳解5. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)6. python 爬取京東指定商品評論并進(jìn)行情感分析7. python基礎(chǔ)之匿名函數(shù)詳解8. Python獲取B站粉絲數(shù)的示例代碼9. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效
