python logging模組使用

2022-08-19 01:45:09 字數 4473 閱讀 2536

在執行自動化指令碼過程中,我們常常需要監控測試用例的執行過程或者測試用例的結果。之前我們往往會將**的執行結果通過print輸出到控制台顯示,控制台顯示資訊的最大弊端就是不能儲存結果。所有當需要將結果顯示時我們常常會使用logging日誌的方式,相對與print的方式,日誌具有以下好處

1.能將資訊分類,可以去設定輸出資料的等級,這樣可以不用顯示大量的除錯資訊

2.print只能將資訊顯示在控制台,而日誌可以將資訊顯示在控制台以及指定的log檔案中。(甚至能顯示在多個檔案中)

3.能設定輸入資訊的格式。print只能僅僅的將內容列印出來,而日誌我們可以設定時間,內容等等格式

​ logging模組是python內建的標準模組,主要用於輸出執行日誌,可以設定輸出日誌的等級、日誌儲存路徑、日誌檔案回滾等;

首先安裝logging第三方模組

pip  logging

配置logging基本的設定,然後在控制台輸出日誌

import logging  

logging.debug('debug message')  

logging.info('info message')  

logging.warning('warning message')  

logging.error('error message')  

logging.critical('critical message')  

warning:root:warning message

error:root:error message

critical:root:critical message

​ 可見,預設情況下python的logging模組將日誌列印到了標準輸出中,且只顯示了大於等於warning級別的日誌, 這說明預設的日誌級別設定為warning(日誌級別等級critical > error > warning > info > debug > notset),

預設的日誌格式為:日誌級別: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(後邊會具體講解handler的概念),這樣日誌會被儲存在指定的檔案中。

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

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

- -datefmt:    指定日期時間格式。

-level:       設定日誌級別

典型的日誌記錄的步驟是這樣的:

建立logger

建立handler(streamhandler與filehandler)

定義formatter

給handler新增formatter

給logger新增handler

import

logging

logger = logging.getlogger()

logger.setlevel(level =logging.info)

#建立乙個filehandler

handler = logging.filehandler("

log.txt

",encoding='utf8'

)handler.setlevel(logging.info)

formatter = logging.formatter('

%(asctime)s - %(name)s - %(levelname)s - %(message)s')

handler.setformatter(formatter)

#建立乙個streamhandler

console =logging.streamhandler()

console.setlevel(logging.info)

logger.addhandler(handler)

logger.addhandler(console)

logger.info(

"start print log")

logger.debug(

"do something")

logger.warning(

"something maybe fail.")

logger.info(

"finish

")

附錄:日誌封裝檔案

#

封裝日誌

defget_log(self):

#建立乙個logger

logger =logging.getlogger()

logger.setlevel(logging.info)

#設定日誌存放路徑,日誌檔名

#獲取本地時間,轉換為設定的格式

rq = time.strftime('

%y%m%d%h%m

', time.localtime(time.time()))

#通日誌存放路

all_log_path = '

logs/all_logs/

'error_log_path ='

logs/error_logs/'#

設定日誌檔名

all_log_name = all_log_path + rq + '

.log

'error_log_name = error_log_path + rq + '

.log'#

建立handler

#建立乙個handler寫入所有日誌

fh = logging.filehandler(all_log_name, encoding='

utf8')

fh.setlevel(logging.info)

#建立乙個handler寫入錯誤日誌

eh = logging.filehandler(error_log_name, encoding="

utf8")

eh.setlevel(logging.error)

#建立乙個handler輸出到控制台

ch =logging.streamhandler()

ch.setlevel(logging.info)

#定義日誌輸出格式

#以時間-日誌器名稱-日誌級別-日誌內容的形式展示

all_log_formatter = logging.formatter('

%(asctime)s - %(name)s - %(levelname)s - %(message)s')

#以時間-日誌器名稱-日誌級別-檔名-函式行號-錯誤內容

error_log_formatter =logging.formatter(

'%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(lineno)s - %(message)s')

#將定義好的輸出形式新增到handler

fh.setformatter(all_log_formatter)

ch.setformatter(all_log_formatter)

eh.setformatter(error_log_formatter)

#給logger新增handler

logger.addhandler(fh)

logger.addhandler(eh)

logger.addhandler(ch)

return logger

Python logging模組學習

import logging 日誌級別列表,預設為logging.warning levels logging.notset,logging.debug,logging.info,logging.warning,logging.error,logging.critical log format as...

python logging模組簡介

logging模組是python內建的標準模組,主要用於輸出執行日誌,可以設定輸出日誌的等級 日誌儲存路徑 日誌檔案回滾等。相對於print,該模組具有可以決定在列印什麼級別的資訊和將資訊輸出放置在什麼地方的優點。配置logging的基本設定,並在控制台輸出 import logging loggi...

Python logging日誌模組

1.日誌的級別 日誌一共分成5個等級,從低到高分別是 1 debug 2.info 3.warning 4.error 5.critical說明 這5個等級,也分別對應5種打日誌的方法 debug info warning error critical。預設的是 warning,當在warning或...