Python的logging模塊基本用法
在服務(wù)器部署時(shí),往往都是在后臺運(yùn)行。當(dāng)程序發(fā)生特定的錯(cuò)誤時(shí),我希望能夠在日志中查詢。因此這里熟悉以下 logging 模塊的用法。
logging 模塊定義了報(bào)告錯(cuò)誤和狀態(tài)信息的標(biāo)準(zhǔn) API。
logging 的組件日志系統(tǒng)有 4 個(gè)相互交互的組件。我們需要使用 Logger 實(shí)例來向日志添加信息。觸發(fā)日志會(huì)創(chuàng)建一個(gè) LogRecord,用于內(nèi)存中存儲信息。Logger 可能有很多 Handler 對象,用于接收和處理日志記錄。Handler 使用 Formatter 來輸出日志記錄。
向文件輸入日志大多數(shù)應(yīng)用都是把日志輸入到文件。使用 basicConfig() 函數(shù)可以設(shè)置默認(rèn)的 handler,讓日志輸入到文件。
#!/usr/bin/env python# -*- coding: utf-8 -*-import loggingLOG_FILENAME = ’log.txt’logging.basicConfig( filename=LOG_FILENAME, level=logging.DEBUG,)logging.debug(’hello logging!’)with open(LOG_FILENAME, ’rt’) as f: body = f.read()print(’FILE: ’)print(body)
運(yùn)行腳本后輸出如下:
FILE: DEBUG:root:hello logging!
日志文件的循環(huán)要讓每次程序運(yùn)行時(shí),生成一個(gè)新的文件,需要向 basicConfig() 傳一個(gè)值為 w 的 filemode 參數(shù)。還有一個(gè)更方便的方法,就是使用 RotatingFileHandler,可以同時(shí)自動(dòng)創(chuàng)建文件和保存舊文件。
#!/usr/bin/env python# -*- coding: utf-8 -*-import globimport logging.handlersLOG_FILENAME = ’log.txt’my_logger = logging.getLogger(’SpecificLogger’)my_logger.setLevel(logging.DEBUG)# Add the log message handler to the loggerhandler = logging.handlers.RotatingFileHandler( LOG_FILENAME, maxBytes=20, backupCount=5,)my_logger.addHandler(handler)# Log some messagesfor i in range(20): my_logger.debug(f’i = {i}’)# See what files are createdlog_files = glob.glob(f’{LOG_FILENAME}*’)for filename in sorted(log_files): print(filename)
運(yùn)行腳本后輸出如下:
log.txtlog.txt.1log.txt.2log.txt.3log.txt.4log.txt.5
可以返現(xiàn),log.txt 存儲的都是最新的內(nèi)容,logging 會(huì)自動(dòng)地對這些文件進(jìn)行重命名。
信息顯示的級別logging 有不同的日志級別。
級別(level) 值(value) CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 UNSET 0
日志可以只在某一級別之上的情況才會(huì)觸發(fā)。
#!/usr/bin/env python# -*- coding: utf-8 -*-import loggingimport syslevel = int(sys.argv[1])logging.basicConfig( level=level)logging.debug(’debug message’)logging.info(’info message’)logging.warning(’warning message’)logging.error(’error message’)logging.critical(’critical message’)
$ python logging_level.py 10DEBUG:root:debug messageINFO:root:info messageWARNING:root:warning messageERROR:root:error messageCRITICAL:root:critical message$ python logging_level 40ERROR:root:error messageCRITICAL:root:critical message命名 logging 實(shí)例
#!/usr/bin/env python# -*- coding: utf-8 -*-import logginglogging.basicConfig( level=logging.WARNING)logger1 = logging.getLogger(’package1.module1’)logger2 = logging.getLogger(’package2.module2’)logger1.warning(’hello 1’)logger2.warning(’hello 2’)
運(yùn)行腳本后輸出:
WARNING:package1.module1:hello 1WARNING:package2.module2:hello 2
以上就是Python的logging模塊基本用法的詳細(xì)內(nèi)容,更多關(guān)于Python logging模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 輕松學(xué)習(xí)XML教程2. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器3. css代碼優(yōu)化的12個(gè)技巧4. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法6. xpath簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理7. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法8. jsp EL表達(dá)式詳解9. 解析原生JS getComputedStyle10. jsp cookie+session實(shí)現(xiàn)簡易自動(dòng)登錄
