python logging日志模塊的詳解
python logging日志模塊的詳解
日志級別
日志一共分成5個等級,從低到高分別是:DEBUG INFO WARNING ERROR CRITICAL。 DEBUG:詳細的信息,通常只出現(xiàn)在診斷問題上 INFO:確認一切按預期運行 WARNING:一個跡象表明,一些意想不到的事情發(fā)生了,或表明一些問題在不久的將來(例如。磁盤空間低”)。這個軟件還能按預期工作。 ERROR:更嚴重的問題,軟件沒能執(zhí)行一些功能 CRITICAL:一個嚴重的錯誤,這表明程序本身可能無法繼續(xù)運行 這5個等級,也分別對應5種打日志的方法: debug 、info 、warning 、error 、critical。默認的是WARNING,當在WARNING或之上時才被跟蹤。
日志格式說明
logging.basicConfig函數(shù)中,可以指定日志的輸出格式format,這個參數(shù)可以輸出很多有用的信息,如上例所示: %(levelno)s: 打印日志級別的數(shù)值 %(levelname)s: 打印日志級別名稱 %(pathname)s: 打印當前執(zhí)行程序的路徑,其實就是sys.argv[0] %(filename)s: 打印當前執(zhí)行程序名 %(funcName)s: 打印日志的當前函數(shù) %(lineno)d: 打印日志的當前行號 %(asctime)s: 打印日志的時間 %(thread)d: 打印線程ID %(threadName)s: 打印線程名稱 %(process)d: 打印進程ID %(message)s: 打印日志信息 我在工作中給的常用格式在前面已經(jīng)看到了。就是: format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s' 這個格式可以輸出日志的打印時間,是哪個模塊輸出的,輸出的日志級別是什么,以及輸入的日志內(nèi)容。
日志輸出
有兩種方式記錄跟蹤,一種輸出控制臺,另一種是記錄到文件中,如日志文件。
將日志輸出到控制臺
在a.py寫入以下信息
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
【解析】
通過logging.basicConfig函數(shù)對日志的輸出格式及方式做相關配置,上面代碼設置日志的輸出等級是WARNING級別,意思是WARNING級別以上的日志才會輸出。另外還制定了日志輸出的格式。
將日志輸出到文件
在logging.basicConfig函數(shù)中設置好輸出文件的文件名和寫文件的模式。
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') 運行之后,打開該文件./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
通過配置文件設置日志模式
https://docs.python.org/2/library/logging.config.html#logging.config.dictConfig
dictconfig比fileconfig要更新
#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')
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
python爬取之json、pickle與shelve庫的深入講解
這篇文章主要給大家介紹了關于python爬取之json、pickle與shelve庫的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03python驗證碼識別教程之灰度處理、二值化、降噪與tesserocr識別
這篇文章主要給大家介紹了關于python驗證碼識別教程之灰度處理、二值化、降噪與tesserocr識別的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-06-06Python開發(fā)如何在ubuntu 15.10 上配置vim
這篇文章主要介紹了Python開發(fā)如何在ubuntu 15.10 上配置vim 的相關資料,需要的朋友可以參考下2016-01-01Python實現(xiàn)報警信息實時發(fā)送至郵箱功能(實例代碼)
這篇文章主要介紹了Python實現(xiàn)報警信息實時發(fā)送至郵箱,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11Python基于FTP模塊實現(xiàn)ftp文件上傳操作示例
這篇文章主要介紹了Python基于FTP模塊實現(xiàn)ftp文件上傳操作,結(jié)合實例形式分析了Python引入ftp模塊及相關設置、文件傳輸?shù)炔僮骷记?需要的朋友可以參考下2018-04-04Pytorch:torch.diag()創(chuàng)建對角線張量方式
這篇文章主要介紹了Pytorch:torch.diag()創(chuàng)建對角線張量方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06