python之路 logging模組

2022-03-30 18:37:05 字數 2994 閱讀 4801

import logging  

logging.debug('debug message')   #bug

logging.info('info message')    #資訊

logging.warning('warning message') # 警告

logging.error('error message')   #錯誤

logging.critical('critical message')#至關重要的

預設情況下python的logging模組將日誌列印到了標準輸出中,且只顯示了大於等於warning級別的日誌,這說明預設的日誌級別設定為warning(日誌級別等級critical > error > warning > info > debug),預設的日誌格式為日誌級別:logger名稱:使用者輸出訊息。

靈活配置日誌級別,日誌格式,輸出位置:

import logging  

logging.basicconfig(level=logging.debug,

format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

datefmt='%a, %d %b %y %h:%m:%s',

filename='/tmp/test.log',

filemode='w')

logging.debug('debug message')

logging.info('info message')

logging.warning('warning message')

logging.error('error message')

logging.critical('critical message')

配置引數:

logging.basicconfig()函式中可通過具體引數來更改logging模組預設行為,可用引數有:

filename:用指定的檔名建立filedhandler,這樣日誌會被儲存在指定的檔案中。

filemode:檔案開啟方式,在指定了filename時使用這個引數,預設值為「a」還可指定為「w」。

format:指定handler使用的日誌顯示格式。

datefmt:指定日期時間格式。

level:設定rootlogger(後邊會講解具體概念)的日誌級別

stream:用指定的stream建立streamhandler。可以指定輸出到sys.stderr,sys.stdout或者檔案(f=open(『test.log』,』w』)),預設為sys.stderr。若同時列出了filename和stream兩個引數,則stream引數會被忽略。

format引數中可能用到的格式化串:

%(name)s logger的名字

%(levelno)s 數字形式的日誌級別

%(levelname)s 文字形式的日誌級別

%(pathname)s 呼叫日誌輸出函式的模組的完整路徑名,可能沒有

%(filename)s 呼叫日誌輸出函式的模組的檔名

%(module)s 呼叫日誌輸出函式的模組名

%(funcname)s 呼叫日誌輸出函式的函式名

%(lineno)d 呼叫日誌輸出函式的語句所在的**行

%(created)f 當前時間,用unix標準的表示時間的浮 點數表示

%(relativecreated)d 輸出日誌資訊時的,自logger建立以 來的毫秒數

%(asctime)s 字串形式的當前時間。預設格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒

%(thread)d 執行緒id。可能沒有

%(threadname)s 執行緒名。可能沒有

%(process)d 程序id。可能沒有

%(message)s使用者輸出的訊息

view code

import logging

logger = logging.getlogger()

# 建立乙個handler,用於寫入日誌檔案

fh = logging.filehandler('test.log',encoding='utf-8')

# 再建立乙個handler,用於輸出到控制台

ch = logging.streamhandler()

formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setlevel(logging.debug)

fh.setformatter(formatter)

ch.setformatter(formatter)

logger.addhandler(fh) #logger物件可以新增多個fh和ch物件 

logger.addhandler(ch)

logger.debug('logger debug message')

logger.info('logger info message')

logger.warning('logger warning message')

logger.error('logger error message')

logger.critical('logger critical message')

logging庫提供了多個元件:logger、handler、filter、formatter。logger物件提**用程式可直接使用的介面,handler傳送日誌到適當的目的地,filter提供了過濾日誌資訊的方法,formatter指定日誌顯示格式。另外,可以通過:logger.setlevel(logging.debug)設定級別,當然,也可以通過

fh.setlevel(logging.debug)單對檔案流設定某個級別。

Python 學習之路 Logging

logging.debug every step log logging.info history logging.warn prompt something 輸出 warning root prompt something 日誌預設輸出到螢幕,並且只輸出warning 包括warning 以上級別...

Python 日誌 logging 模組

對於小型專案而言,大家習慣於使用print語句列印資訊到終端進行除錯,然而,當專案 量擴大到了一定級別後,列印除錯的方法就顯得很凌亂了,更重要的是,當debug完成後,甄別並刪除 或注釋 除錯用的列印語句變得非常令人頭痛。而使用日誌模組則能很好地解決這些問題。日誌 logging 是在程式執行過程中...

python 日誌使用logging

將日誌列印入檔案,同時列印在控制台 logfile.py coding utf 8 import sys import logging from logging.handlers import timedrotatingfilehandler def getlogconfig name defaul...