| 級別 | 何時使用 |
|---|---|
| DEBUG | 細節(jié)信息,僅當(dāng)診斷問題時適用。 |
| INFO | 確認程序按預(yù)期運行 |
| WARNING | 表明有已經(jīng)或即將發(fā)生的意外(例如:磁盤空間不足)。程序仍按預(yù)期進行 |
| ERROR | 由于嚴重的問題,程序的某些功能已經(jīng)不能正常執(zhí)行 |
| CRITICAL | 嚴重的錯誤,表明程序已不能繼續(xù)執(zhí)行 |
import logging
logging.warning('Watch out!')
logging.info('I told you so')
import logging
# 保存文件為example.log,記錄等級為DEBUG,即只記錄DENBUG及以上的日志
# 輸出格式為 2019-19-06 18:47:06 - WARNING - And this, too
logging.basicConfig(
filename='example.log',
filemode='w',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%d-%d %H:%M:%S'
)
logging.debug('This message should go to the log file')
logging.info('So shoul this')
logging.warning('And this, too')
filename日志文件路徑
filemode記錄日志文件的模式,w為每次啟動程序都創(chuàng)建一個全新的文件記錄, a表示追加到文件末尾, 默認為a
level記錄日志的等級
format日志輸出的格式
datefmt日志輸出時間的格式
[loggers]
# 配置日志對象名, 默認為root
keys=root, poj
[handlers]
# 日志配置名對象名
keys=writeFileHandlers
[formatters]
# 日志輸出格式對象名
keys=writeFileFormatters
[logger_root]
level=DEBUG
handlers=writeFileHandlers
[logger_poj]
level=DEBUG
handlers=writeFileHandlers
qualname=writeFileFormatters
propagate=0
[logger_leetcode]
level=DEBUG
handlers=writeFileHandlers
qualname=writeFileFormatters
propagate=0
[handler_writeFileHandlers]
# 設(shè)置writeFileHandlers對象的配置
class=FileHandler
level=DEBUG
formatter=writeFileFormatters
# 記錄在文件中,以追加的形式
args=("demo.log", "a")
[formatter_writeFileFormatters]
設(shè)置writeFileHandlers對象的輸出配置
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%d-%m %H:%M:%S
import logging.config
# 加載配置文件
logging.config.fileConfig('logging.conf')
# 獲取日志對象名為poj的
logger = logging.getLogger("poj")
logger.debug('This message should go to the log file')
logger.info('So shoul this')
logger.warning('And this, too')
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
標(biāo)簽:衡水 呼和浩特 江蘇 中山 畢節(jié) 湖州 股票 駐馬店
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python 解決logging功能使用過程中遇到的一個問題》,本文關(guān)鍵詞 Python,解決,logging,功能,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。