python/271572.html">python/277831.html">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 message2017-03-16 16:58:11,266 - a.py[line:11] - ERROR: this is an loggging error message2017-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 message2015-05-21 17:30:20,282 - log.py[line:13] - ERROR: this is an loggging error message2015-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=DEBUGhandlers=hand01,hand02[logger_example01]handlers=hand01,hand02qualname=example01propagate=0[logger_example02]handlers=hand01,hand03qualname=example02propagate=0###############################################[handlers]keys=hand01,hand02,hand03[handler_hand01]class=StreamHandlerlevel=INFOformatter=form02args=(sys.stderr,)[handler_hand02]class=FileHandlerlevel=NOTSETformatter=form01args=('myapp.log', 'a')[handler_hand03]class=handlers.RotatingFileHandlerlevel=INFOformatter=form02args=('myapp.log', 'a', 10*1024*1024, 5)###############################################[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)sdatefmt=%a, %d %b %Y %H:%M:%S[formatter_form02]format=%(name)-12s: %(levelname)-8s %(message)sdatefmt=主函數(shù)
import loggingimport logging.configlogging.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ì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選