python日誌模組logging

2021-10-23 05:24:49 字數 3378 閱讀 2371

在python專案中可以使用logging模組來管理記錄的日誌。

簡單配置

使用basicconfig()方法簡單地配置logger即可滿足基本的需求。

basicconfig()方法引數如下:

引數名稱

引數說明

filename

日誌輸出檔案的檔名

filemode

開啟檔案的模式,有r(+)、w(+)和a(+)

format

日誌輸出的格式

datefat

日誌輸出的日期格式

style

格式佔位符

level

設定日誌輸出級別

stream

定義輸出流,不能與filename引數同時使用

handles

定義處理器,用來建立handler物件,不能和filename、stream引數同時使用

示例**如下:

import logging

logging.basicconfig(filename="test.log", filemode="w", format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%d-%m-%y %h:%m:%s", level=logging.debug)

logging.debug('this is a debug message')

logging.info('this is an info message')

logging.warning('this is a warning message')

logging.error('this is an error message')

logging.critical('this is a critical message')

#使用logging輸出日誌

logging.debug('this is a debug message')

logging.info('this is an info message')

logging.warning('this is a warning message')

logging.error('this is an error message')

logging.critical('this is a critical message')

自定義logger

除了使用basicconfig()方式配置logger之外,我們還可以自定義logger。示例**:

import logging

import logging.handlers

def getlogger():

logger = logging.getlogger("logger")

handler1 = logging.streamhandler() #獲取輸出流handler,用於將日誌輸出到命令列

handler2 = logging.filehandler(filename="segmentation.log") #獲取檔案handler,用於將日誌輸出到檔案

logger.setlevel(logging.debug) #設定日誌等級

handler1.setlevel(logging.debug)

handler2.setlevel(logging.debug)

formatter = logging.formatter("%(asctime)s %(name)s %(levelname)s %(message)s") #設定日誌輸出的格式

handler1.setformatter(formatter)

handler2.setformatter(formatter)

logger.addhandler(handler1) #日誌輸出到handler

logger.addhandler(handler2)

return logger

#使用logger輸出日誌

logger = getlogger()

logger.debug('this is a debug message')

logger.info('this is an info message')

logger.warning('this is a warning message')

logger.error('this is an error message')

logger.critical('this is a critical message')

其中,輸出格式formatter變數如下:

變數格式

描述asctime

%(sactime)s

將日誌時間構造成可讀的形式

name

%(name)

日誌物件的名稱

filename

%(filename)s

不包含路徑的檔名

pathname

%(pathname)s

包含路徑的檔名

funcname

%(funcname)s

日誌記錄所在的函式名

levelname

%(levelname)s

日誌級別名稱

message

%(message)s

具體的日誌資訊

lineno

%(lineno)d

日誌記錄所在的行號

pathname

%(pathname)s

完整路徑

process

%(process)d

當前程序id

processname

%(processname)s

當前程序名稱

thread

%(thread)d

當前執行緒id

threadname

%(threadname)s

當前執行緒名稱

logger單例模式

乙個系統只有乙個logger物件,並且該物件不能被直接例項化,這裡使用的是單例模式,獲取物件的方法為getlogger。

我們可以建立多個logger物件,但是真正輸出日誌的是根logger物件。每個logger物件都可以設定乙個名字,例如:

logger = logging.getlogger(__name__)    #__name__代表當前模組的名稱(預設為__main__)
更多關於logging的內容,請檢視官方文件

PythonStudy 日誌模組 logging

日誌 日之石日常的流水,將程式執行過程中的狀態或資料盡心記錄,一般是記錄到日誌檔案當中的。在正常的專案之中,專案的執行的一些列印資訊,採用logging列印到檔案當中,這個過程就稱作為 日誌記錄模組 以下為預設的操作日誌模組 匯入日誌模組 import logging logging為預設列印者,是...

python日誌模組

logging.debug 10 logging.info 20 logging.warning 30 logging.error 40 logging.critical 50預設級別為warning 預設輸出位置為控制台 import logging logging.basicconfig 可用引...

python 日誌模組

在軟體或者系統發生錯誤時可以通過日誌快速定位到錯誤,從而定位問題,解決問題。logging模組提供的日誌記錄函式所使用的日誌器設定的日誌級別是warning,因此只有warning級別的日誌記錄以及大於它的error和critical級別的日誌記錄被輸出了,而小於它的debug和info級別的日誌記...