Python的logging模塊基本用法
在服務(wù)器部署時(shí),往往都是在后臺(tái)運(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)存中存儲(chǔ)信息。Logger 可能有很多 Handler 對(duì)象,用于接收和處理日志記錄。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 存儲(chǔ)的都是最新的內(nèi)容,logging 會(huì)自動(dòng)地對(duì)這些文件進(jìn)行重命名。
信息顯示的級(jí)別logging 有不同的日志級(jí)別。
級(jí)別(level) 值(value) CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 UNSET 0
日志可以只在某一級(jí)別之上的情況才會(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模塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測(cè)試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
