python中logging庫的使用總結,pythonlogging
前言
最近因為工作的需要,在寫一些python指令碼,總是使用print來列印資訊感覺很low,所以抽空研究了一下python的logging庫,來優雅的來列印和記錄日誌,下面話不多說了,來一起看看詳細的介紹吧。
一、簡單的將日誌列印到螢幕:
import logginglogging.debug('This is debug message') #debuglogging.info('This is info message') #infologging.warning('This is warning message') #warn
螢幕上列印:WARNING:root:This is warning message
預設情況下,會列印WARNING層級的日誌
- DEBUG:詳細資料,調試資訊。
- INFO:確認一切按預期運行。
- WARNING:表明發生了一些意外,或者不久的將來會發生問題(如‘磁碟滿了')。軟體還是在正常工作。
- ERROR:由於更嚴重的問題,軟體已不能執行一些功能了。
- CRITICAL:嚴重錯誤,表明軟體已不能繼續運行了。
二、通過basicConfig函數來對日誌的輸入格式和方法進行配置
import logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %a %H:%M:%S', filename='test.log', filemode='w') logging.debug('This is debug message')logging.info('This is info message')logging.warning('This is warning message')
以上代碼不會在螢幕上列印日誌,而是會在目前的目錄產生test.log的記錄檔,日誌被列印在記錄檔中:
2017-10-16 Mon 10:05:17 testlogging.py[line:25] DEBUG This is debug message
2017-10-16 Mon 10:05:17 testlogging.py[line:26] INFO This is info message
2017-10-16 Mon 10:05:17 testlogging.py[line:27] WARNING This is warning message
- logging.basicConfig函數各參數:
- filename: 指定記錄檔名
- filemode: 和file函數意義相同,指定記錄檔的開啟模式,'w'或'a'
- format: 指定輸出的格式和內容,format可以輸出很多有用資訊,如上例所示:
%(levelno)s: 列印記錄層級的數值 %(levelname)s: 列印記錄層級名稱 %(pathname)s: 列印當前執行程式的路徑,其實就是sys.argv[0] %(filename)s: 列印當前執行程式名 %(funcName)s: 列印日誌的當前函數 %(lineno)d: 列印日誌的當前行號 %(asctime)s: 列印日誌的時間 %(thread)d: 列印線程ID %(threadName)s: 列印線程名稱 %(process)d: 列印進程ID %(message)s: 列印日誌資訊
- datefmt: 指定時間格式,同time.strftime()
- level: 設定記錄層級,預設為logging.WARNING
- stream: 指定將日誌的輸出資料流,可以指定輸出到sys.stderr,sys.stdout或者檔案,預設輸出到sys.stderr,當stream和filename同時指定時,stream被忽略
三、同時將日誌輸出到螢幕和記錄檔
#定義一個StreamHandler,將INFO層級或更高的日誌資訊列印到標準錯誤,並將其添加到當前的Tlog對象console = logging.StreamHandler()console.setLevel(logging.INFO)formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')console.setFormatter(formatter)logging.getLogger('').addHandler(console)
插入以上代碼就可以同時在螢幕上列印出日誌
螢幕上列印:
root : INFO This is info message
root : WARNING This is warning message
檔案中列印:
2017-10-16 Mon 10:20:07 testlogging.py[line:46] DEBUG This is debug message
2017-10-16 Mon 10:20:07 testlogging.py[line:47] INFO This is info message
2017-10-16 Mon 10:20:07 testlogging.py[line:48] WARNING This is warning message
四、通過設定檔配置日誌
#logger.conf[loggers]#定義logger模組,root是父類,必需存在的,其它的是自訂。 keys=root,infoLogger,warnlogger[logger_root] level=DEBUG #level 層級,層級有DEBUG、INFO、WARNING、ERROR、CRITICALhandlers=infohandler,warnhandler #handlers 處理類,可以有多個,用逗號分開[logger_infoLogger] #[logger_xxxx] logger_模組名稱handlers=infohandlerqualname=infoLogger #qualname logger名稱,應用程式通過 logging.getLogger擷取。對於不能擷取的名稱,則記錄到root模組。propagate=0 #propagate 是否繼承父類的log資訊,0:否 1:是[logger_warnlogger]handlers=warnhandlerqualname=warnloggerpropagate=0################################################定義handler[handlers]keys=infohandler,warnhandler[handler_infohandler]class=StreamHandler #class handler類名level=INFO #level 記錄層級formatter=form02 #formatter,下面定義的formatterargs=(sys.stdout,) #args handler初始化函數參數[handler_warnhandler]class=FileHandlerlevel=WARNformatter=form01args=('logs/deploylog.log', 'a')################################################ 定義格式化輸出[formatters]keys=form01,form02[formatter_form01]format=%(message)s %(asctime)sdatefmt=%Y-%m-%d %H:%M:%S[formatter_form02]format=%(asctime)s %(levelname)s %(message)sdatefmt=%Y-%m-%d %H:%M:%S
日誌格式
- %(asctime)s 年-月-日 時-分-秒,毫秒 2013-04-26 20:10:43,745
- %(filename)s 檔案名稱,不含目錄
- %(pathname)s 目錄名,完整路徑
- %(funcName)s 函數名
- %(levelname)s 層級名
- %(lineno)d 行號
- %(module)s 模組名
- %(message)s 訊息體
- %(name)s 日誌模組名
- %(process)d 進程id
- %(processName)s 進程名
- %(thread)d 線程id
- %(threadName)s 線程名
測試組態檔案
from logging.config import fileConfigfileConfig('logger.conf')logger=logging.getLogger('infoLogger')logger.info('test1')logger_error=logging.getLogger('warnhandler')logger_error.warn('test5')
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對幫客之家的支援。