久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python的logging模塊基本用法

瀏覽:145日期:2022-07-01 11:05:21

在服務(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í)例來(lái)向日志添加信息。觸發(fā)日志會(huì)創(chuàng)建一個(gè) LogRecord,用于內(nèi)存中存儲(chǔ)信息。Logger 可能有很多 Handler 對(duì)象,用于接收和處理日志記錄。Handler 使用 Formatter 來(lái)輸出日志記錄。

向文件輸入日志

大多數(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)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 波密县| 马龙县| 南汇区| 天水市| 云霄县| 东光县| 沈阳市| 专栏| 黄陵县| 长春市| 梁河县| 芜湖县| 荃湾区| 恩施市| 保定市| 合山市| 遵化市| 临泉县| 柳江县| 基隆市| 陵水| 建湖县| 江门市| 沐川县| 剑阁县| 祥云县| 丰都县| 达州市| 天等县| 大足县| 扶沟县| 汶上县| 抚远县| 通许县| 广饶县| 西盟| 桑植县| 瑞昌市| 乡宁县| 泸水县| 大丰市|