標籤:sys 時間 對象 通過 icc ack logging std img
1、將日誌直接輸出到螢幕
import logginglogging.debug(‘This is debug message‘)logging.info(‘This is info message‘)logging.warning(‘This is warning message‘)# 預設情況下,logging將日誌列印到螢幕,記錄層級為WARNING;#output====================================# WARNING:root:This is warning message
2.通過logging.basicConfig函數對日誌的輸出格式及方式做相關配置
import logginglogging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=‘myapp.log‘, filemode=‘w‘) logging.debug(‘This is debug message‘)logging.info(‘This is info message‘)logging.warning(‘This is warning message‘)#./myapp.log檔案中內容為:#Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message#Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message#Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message
logging.basicConfig參數:
#logging.basicConfig函數各參數:filename: 指定記錄檔名filemode: 和file函數意義相同,指定記錄檔的開啟模式,‘w‘或‘a‘format: 指定輸出的格式和內容,format可以輸出很多有用資訊,如上例所示: %(levelno)s: 列印記錄層級的數值 %(levelname)s: 列印記錄層級名稱 %(pathname)s: 列印當前執行程式的路徑,其實就是sys.argv[0] %(filename)s: 列印當前執行程式名 %(funcName)s: 列印日誌的當前函數 %(lineno)d: 列印日誌的當前行號 %(asctime)s: 列印日誌的時間 %(thread)d: 列印線程ID %(threadName)s: 列印線程名稱 %(process)d: 列印進程ID %(message)s: 列印日誌資訊datefmt: 指定時間格式,同time.strftime()level: 設定記錄層級,預設為logging.WARNINGstream: 指定將日誌的輸出資料流,可以指定輸出到sys.stderr,sys.stdout或者檔案,預設輸出到sys.stderr,當stream和filename同時指定時,stream被忽略
3、將日誌同時輸出到多個Handler
先定義一個住handler,並使用addHander()添加到主handler,實現日誌輸出到多個handler.
a、同時輸出到檔案和螢幕
import logging#設定一個basicConfig只能輸出到一個Handlerlogging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=‘myapp.log‘, filemode=‘w‘)#定義一個StreamHandler,將INFO層級或更高的日誌資訊列印到標準錯誤,並將其添加到當前的Tlog對象#console = logging.StreamHandler()console.setLevel(logging.INFO)formatter = logging.Formatter(‘%(name)-12s: %(levelname)-8s %(message)s‘)console.setFormatter(formatter)logging.getLogger(‘‘).addHandler(console)#輸出到檔案的log層級為debug,輸出到stream的log層級為infologging.debug(‘This is debug message‘)logging.info(‘This is info message‘)logging.warning(‘This is warning message‘)
b、添加一個handler:輸出到檔案,並根據檔案大小滾動儲存
在a的基礎上添加一個handler
from logging.handlers import RotatingFileHandler#定義一個RotatingFileHandler,最多備份5個記錄檔,每個記錄檔最大10MRthandler = RotatingFileHandler(‘myapp.log‘, maxBytes=10*1024*1024,backupCount=5)Rthandler.setLevel(logging.INFO)formatter = logging.Formatter(‘%(name)-12s: %(levelname)-8s %(message)s‘)Rthandler.setFormatter(formatter)logging.getLogger(‘‘).addHandler(Rthandler)
logging幾種Handler類型:
logging.StreamHandler(預設): 日誌輸出到流,可以是sys.stderr、sys.stdout或者檔案logging.FileHandler: 日誌輸出到檔案logging.handlers.RotatingFileHandler 日誌輸出到檔案,基於檔案大小滾動儲存日誌logging.handlers.TimedRotatingFileHandler 日誌輸出到檔案,基於時間周期滾動儲存日誌logging.handlers.SocketHandler: 遠程輸出日誌到TCP/IP socketslogging.handlers.DatagramHandler: 遠程輸出日誌到UDP socketslogging.handlers.SMTPHandler: 遠程輸出日誌到郵件地址logging.handlers.SysLogHandler: 日誌輸出到sysloglogging.handlers.NTEventLogHandler: 遠程輸出日誌到Windows NT/2000/XP的事件記錄logging.handlers.MemoryHandler: 日誌輸出到記憶體中的制定bufferlogging.handlers.HTTPHandler: 通過"GET"或"POST"遠程輸出到HTTP伺服器
4、通過設定檔配置logger
a、定義設定檔logger.conf
#logger.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=DEBUGformatter=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=
b、logging.config擷取配置
import loggingimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example01")logger.debug(‘This is debug message‘)logger.info(‘This is info message‘)logger.warning(‘This is warning message‘)
import loggingimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example02")logger.debug(‘This is debug message‘)logger.info(‘This is info message‘)logger.warning(‘This is warning message‘)View Code
python 內建模組之logging