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 對象,用于接收和處理日志記錄。Handler 使用 Formatter 來輸出日志記錄。
向文件輸入日志
大多數(shù)應(yīng)用都是把日志輸入到文件。使用 basicConfig() 函數(shù)可以設(shè)置默認(rèn)的 handler,讓日志輸入到文件。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
LOG_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 glob
import logging.handlers
LOG_FILENAME = 'log.txt'
my_logger = logging.getLogger('SpecificLogger')
my_logger.setLevel(logging.DEBUG)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME,
maxBytes=20,
backupCount=5,
)
my_logger.addHandler(handler)
# Log some messages
for i in range(20):
my_logger.debug(f'i = {i}')
# See what files are created
log_files = glob.glob(f'{LOG_FILENAME}*')
for filename in sorted(log_files):
print(filename)
運(yùn)行腳本后輸出如下:
log.txt
log.txt.1
log.txt.2
log.txt.3
log.txt.4
log.txt.5
可以返現(xiàn),log.txt 存儲(chǔ)的都是最新的內(nèi)容,logging 會(huì)自動(dòng)地對這些文件進(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 logging
import sys
level = 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 10 DEBUG:root:debug message INFO:root:info message WARNING:root:warning message ERROR:root:error message CRITICAL:root:critical message $ python logging_level 40 ERROR:root:error message CRITICAL:root:critical message
命名 logging 實(shí)例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
logging.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 1
WARNING:package2.module2:hello 2
以上就是Python的logging模塊基本用法的詳細(xì)內(nèi)容,更多關(guān)于Python logging模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pandas DataFrame.shift()函數(shù)的具體使用
本文主要介紹了pandas DataFrame.shift()函數(shù)的使用,pandas DataFrame.shift()函數(shù)可以把數(shù)據(jù)移動(dòng)指定的位數(shù),有需要了解pandas DataFrame.shift()用法的朋友可以參考一下2021-05-05
淺談django model postgres的json字段編碼問題
下面小編就為大家分享一篇淺談django model postgres的json字段編碼問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Python實(shí)現(xiàn)備份文件實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)備份文件的方法,可實(shí)現(xiàn)針對各類常見擴(kuò)展名的文件進(jìn)行備份的功能,需要的朋友可以參考下2014-09-09
pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù)詳解
這篇文章主要介紹了pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
使用pyshp包進(jìn)行shapefile文件修改的例子
今天小編就為大家分享一篇使用pyshp包進(jìn)行shapefile文件修改的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python使用pyshp讀寫shp文件的實(shí)現(xiàn)
本文主要介紹了python使用pyshp讀寫shp文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

