python之日誌配置

2021-07-25 12:47:51 字數 4368 閱讀 7775

日誌:日誌是記錄軟體執行狀態的一種方法,對於軟體的除錯等工作有極大作用。

通常我們想將軟體的執行狀態呈現出來,比如輸出到螢幕上,或者寫到檔案中,或者發到網路上等等,這時就需要有我們自己的日誌記錄。在python中的logging日誌庫設計的非常好,它可以幫助我們完成相應的日誌記錄設計。

對於部分人來說logging提供的模組級函式logging.basiconfig()已經足以讓人進行日誌記錄實踐。它的簡單用法如下:

import logging

import mylib

defmain

(): logging.info('started')

mylib.do_something()

logging.info('finished')

if __name__ == '__main__':

main()

# mylib.py

import logging

defdo_something

(): logging.info('doing something')

logging庫採取了模組化的設計,提供了許多元件:記錄器、處理器、過濾器和格式化器。

日誌功能都由呼叫logger類例項的方法執行(以後都稱為loggers)。每個例項都有名字,它們在概念上組織成乙個層級式的命名空間,使用點(.)作為分隔符。例如,名為『scan』的logger是名為『scan.text』、『scan.html』和『scan.pdf』的logger的父節點。

程式設計師可以以三種方式來配置日誌:

本文介紹第一種配置方式

請大家記住,配置日誌就是配置logger的功能,因為logger是提供給應用程式的介面,不管是handler還是filter或者是formatter都是配置來給logger使用的。

logger的配置:

先使用logging.getlogger()生成乙個logger物件,然後使用logger.setlevel()方法指定logger將會處理的最低的安全等級日誌資訊。

再使用logger.addhandler()方法加入乙個handler(logger.removehandler()是移除handler,對於加入handler的數量沒有限制)。

再使用logger.addfilter() 方法加入乙個filter(logger.removefilter()是移除filter)。

handler的配置:

handler一般不會直接例項化來使用,而是繼承它然後再例項化出乙個handler來使用。因為handler的功能就是將日誌資訊傳送到各個地方,如螢幕,檔案,網路等,所以不同的handler類有不同的作用(logging.handlers庫中有各種不同handler)。

例如:

#設定乙個高於debug級別就輸出到螢幕的handler

ch = logging.streamhandler()

ch.setlevel(logging.debug)

#設定乙個高於debug級別就輸出到檔案的handler

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

ch2 = logging.filehandler()

ch2.setlevel(logging.debug) #設定輸出等級要求

ch2.setformatter(formatter) #設定資訊格式

formatter的配置

formatter物件決定日誌訊息最終的順序,結構和內容。

例如:

formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter例項化後就可以傳遞給handler,利用ch2.setformatter(formatter)方法。

filter

filter為日誌配置提供了更細粒度的設定,但是不是必要的所以本文沒有提及。

import logging

import auxiliary_module

logger.setlevel(logging.debug)

# 建立乙個將debug以上資訊輸出到檔案'spam.log'的handler

fh = logging.filehandler('spam.log')

fh.setlevel(logging.debug)

# 建立可以將error資訊輸出到螢幕的hander

ch = logging.streamhandler()

ch.setlevel(logging.error)

# 建立乙個formatter並傳遞給handler

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

fh.setformatter(formatter)

ch.setformatter(formatter)

# 將handler傳遞給logger

logger.addhandler(fh)

logger.addhandler(ch)

logger.info('creating an instance of auxiliary_module.auxiliary')

a = auxiliary_module.auxiliary()

logger.info('created an instance of auxiliary_module.auxiliary')

logger.info('calling auxiliary_module.auxiliary.do_something')

a.do_something()

logger.info('finished auxiliary_module.auxiliary.do_something')

logger.info('calling auxiliary_module.some_function()')

auxiliary_module.some_function()

logger.info('done with auxiliary_module.some_function()')

import logging

# 建立乙個logger

class

auxiliary:

def__init__

(self):

self.logger.info('creating an instance of auxiliary')

defdo_something

(self):

self.logger.info('doing something')

a = 1 + 1

self.logger.info('done doing something')

defsome_function

(): module_logger.info('received a call to "some_function"')

最終輸出:

2005-03-23

creating an instance of auxiliary_module.auxiliary

2005-03-23

creating an instance of auxiliary

2005-03-23

created an instance of auxiliary_module.auxiliary

2005-03-23

calling auxiliary_module.auxiliary.do_something

2005-03-23

doing something

2005-03-23

done doing something

2005-03-23

finished auxiliary_module.auxiliary.do_something

2005-03-23

calling auxiliary_module.some_function()

2005-03-23

received a call to

'some_function'

2005-03-23

done with auxiliary_module.some_function()

python之日誌模組

import logging logging.basicconfig 1.日誌輸出位置 a.中端b.檔案 filename d pyt 學習 python基礎操作 access.log 不指定,預設列印到中端 2.如何自定義日誌格式 format asctime s name s levelname...

Solr之日誌配置 yellowcong

如果部配置日誌,出了錯誤,只能哭了,我剛開始一直報錯,就是不知道錯誤在哪兒,在tomcat的log目錄下,也沒有資料,下面配置的日誌方式是固定日誌檔案大小,滾動輸出,不是一直追加的方式 在tomcat的solr目錄的web inf classes目錄下,建立log4j.properties檔案 按大...

springboot之日誌配置四

配置方式 預設配置檔案配置和引用外部配置檔案配置 一 預設配置檔案配置 不建議使用 不夠靈活,對log4j2等不夠友好 日誌檔名,比如 roncoo.log,或者是 var log roncoo.log logging.file roncoo.log 日誌級別配置,比如 logging.level....