python logging日志模塊的詳解
python logging日志模塊的詳解
日志級(jí)別
日志一共分成5個(gè)等級(jí),從低到高分別是:DEBUG INFO WARNING ERROR CRITICAL。 DEBUG:詳細(xì)的信息,通常只出現(xiàn)在診斷問(wèn)題上 INFO:確認(rèn)一切按預(yù)期運(yùn)行 WARNING:一個(gè)跡象表明,一些意想不到的事情發(fā)生了,或表明一些問(wèn)題在不久的將來(lái)(例如。磁盤(pán)空間低”)。這個(gè)軟件還能按預(yù)期工作。 ERROR:更嚴(yán)重的問(wèn)題,軟件沒(méi)能執(zhí)行一些功能 CRITICAL:一個(gè)嚴(yán)重的錯(cuò)誤,這表明程序本身可能無(wú)法繼續(xù)運(yùn)行 這5個(gè)等級(jí),也分別對(duì)應(yīng)5種打日志的方法: debug 、info 、warning 、error 、critical。默認(rèn)的是WARNING,當(dāng)在WARNING或之上時(shí)才被跟蹤。
日志格式說(shuō)明
logging.basicConfig函數(shù)中,可以指定日志的輸出格式format,這個(gè)參數(shù)可以輸出很多有用的信息,如上例所示: %(levelno)s: 打印日志級(jí)別的數(shù)值 %(levelname)s: 打印日志級(jí)別名稱 %(pathname)s: 打印當(dāng)前執(zhí)行程序的路徑,其實(shí)就是sys.argv[0] %(filename)s: 打印當(dāng)前執(zhí)行程序名 %(funcName)s: 打印日志的當(dāng)前函數(shù) %(lineno)d: 打印日志的當(dāng)前行號(hào) %(asctime)s: 打印日志的時(shí)間 %(thread)d: 打印線程ID %(threadName)s: 打印線程名稱 %(process)d: 打印進(jìn)程ID %(message)s: 打印日志信息 我在工作中給的常用格式在前面已經(jīng)看到了。就是: format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s' 這個(gè)格式可以輸出日志的打印時(shí)間,是哪個(gè)模塊輸出的,輸出的日志級(jí)別是什么,以及輸入的日志內(nèi)容。
日志輸出
有兩種方式記錄跟蹤,一種輸出控制臺(tái),另一種是記錄到文件中,如日志文件。
將日志輸出到控制臺(tái)
在a.py寫(xiě)入以下信息
import logging logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') # use logging logging.info('this is a loggging info message') logging.debug('this is a loggging debug message') logging.warning('this is loggging a warning message') logging.error('this is an loggging error message') logging.critical('this is a loggging critical message') 執(zhí)行上面的代碼將在Console中輸出下面信息: 2017-03-16 16:58:11,266 - a.py[line:10] - WARNING: this is loggging a warning message 2017-03-16 16:58:11,266 - a.py[line:11] - ERROR: this is an loggging error message 2017-03-16 16:58:11,266 - a.py[line:12] - CRITICAL: this is a loggging critical message
【解析】
通過(guò)logging.basicConfig函數(shù)對(duì)日志的輸出格式及方式做相關(guān)配置,上面代碼設(shè)置日志的輸出等級(jí)是WARNING級(jí)別,意思是WARNING級(jí)別以上的日志才會(huì)輸出。另外還制定了日志輸出的格式。
將日志輸出到文件
在logging.basicConfig函數(shù)中設(shè)置好輸出文件的文件名和寫(xiě)文件的模式。
import logging logging.basicConfig(level=logging.WARNING, filename='./log/log.txt', filemode='w', format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') # use logging logging.info('this is a loggging info message') logging.debug('this is a loggging debug message') logging.warning('this is loggging a warning message') logging.error('this is an loggging error message') logging.critical('this is a loggging critical message') 運(yùn)行之后,打開(kāi)該文件./log/log.txt,效果如下: 2015-05-21 17:30:20,282 - log.py[line:12] - WARNING: this is loggging a warning message 2015-05-21 17:30:20,282 - log.py[line:13] - ERROR: this is an loggging error message 2015-05-21 17:30:20,282 - log.py[line:14] - CRITICAL: this is a loggging critical message
通過(guò)配置文件設(shè)置日志模式
https://docs.python.org/2/library/logging.config.html#logging.config.dictConfig
dictconfig比f(wàn)ileconfig要更新
#config.conf ############################################### [loggers] keys=root,example01,example02 [logger_root] level=DEBUG handlers=hand01,hand02 [logger_example01] handlers=hand01,hand02 qualname=example01 propagate=0 [logger_example02] handlers=hand01,hand03 qualname=example02 propagate=0 ############################################### [handlers] keys=hand01,hand02,hand03 [handler_hand01] class=StreamHandler level=INFO formatter=form02 args=(sys.stderr,) [handler_hand02] class=FileHandler level=NOTSET formatter=form01 args=('myapp.log', 'a') [handler_hand03] class=handlers.RotatingFileHandler level=INFO formatter=form02 args=('myapp.log', 'a', 10*1024*1024, 5) ############################################### [formatters] keys=form01,form02 [formatter_form01] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s datefmt=%a, %d %b %Y %H:%M:%S [formatter_form02] format=%(name)-12s: %(levelname)-8s %(message)s datefmt=
主函數(shù)
import logging import logging.config logging.config.fileConfig("/home/razerware/configscript/config.conf") logger = logging.getLogger("example01") logger2 = logging.getLogger("example02") logger.debug('This is debug message') logger.info('This is info message') logger.warning('This is warning message') logger2.debug('This is debug message') logger2.info('This is info message') logger2.warning('This is warning message')
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
python爬取之json、pickle與shelve庫(kù)的深入講解
這篇文章主要給大家介紹了關(guān)于python爬取之json、pickle與shelve庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python驗(yàn)證碼識(shí)別教程之灰度處理、二值化、降噪與tesserocr識(shí)別
這篇文章主要給大家介紹了關(guān)于python驗(yàn)證碼識(shí)別教程之灰度處理、二值化、降噪與tesserocr識(shí)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Python學(xué)習(xí)Turtle庫(kù)畫(huà)對(duì)稱勾股樹(shù)體會(huì)分形驚艷
這篇文章主要為大家介紹了Python學(xué)習(xí)中如何使用Turtle庫(kù)畫(huà)對(duì)稱勾股樹(shù),從而體會(huì)到分形世界的驚艷,文中附含詳細(xì)示例代碼有需要的朋友可以借鑒參考下2021-09-09Python開(kāi)發(fā)如何在ubuntu 15.10 上配置vim
這篇文章主要介紹了Python開(kāi)發(fā)如何在ubuntu 15.10 上配置vim 的相關(guān)資料,需要的朋友可以參考下2016-01-01Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱功能(實(shí)例代碼)
這篇文章主要介紹了Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作示例
這篇文章主要介紹了Python基于FTP模塊實(shí)現(xiàn)ftp文件上傳操作,結(jié)合實(shí)例形式分析了Python引入ftp模塊及相關(guān)設(shè)置、文件傳輸?shù)炔僮骷记?需要的朋友可以參考下2018-04-04解決Jupyter-notebook不彈出默認(rèn)瀏覽器的問(wèn)題
這篇文章主要介紹了解決Jupyter-notebook不彈出默認(rèn)瀏覽器的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Pytorch:torch.diag()創(chuàng)建對(duì)角線張量方式
這篇文章主要介紹了Pytorch:torch.diag()創(chuàng)建對(duì)角線張量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06