1.簡單的將日誌印到螢幕
import logging
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning(Thisessage's') wart logging.warning(Thisessage' )
螢幕上列印:
WARNING:root:This is warning message
預設情況下,logging將日誌列印到螢幕,日誌等級為WARNING;
等級值
WARNING > INFO > DEBUG > NOTSET,當然也可以自己定義日誌等級。
2.透過logging.basicConfig函數對日誌的輸出格式及方式做相關配置
import logging
logging.basicConfig(level=logging.DEBUG,
name % line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b % ',
filemode='w')
logging.debug('This is debug message')
logging.info('T.This) his p.
./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[ :12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message
日誌
logging.basicConfig函數各指定檔案filemode:
和file函數意義相同,指定日誌檔案的開啟模式,'w'或'a'
format:
指定輸出的格式和內容,format可以輸出很多有用信息,如上例所示:
%(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.WARNING
stream:
指定將日誌的輸出流,可以指定輸出到sys.stderr,sys.stdout或文件,預設輸出到sys.stderr,當stream和filename同時指定時,stream被忽略
3.將日誌同時輸出到文件和螢幕
import logging
logging.DEBUG,
s format='%(asctime)s %:name)s format0%(asctime)s %:name)s format]s%)s[]s%)s[ ', datefmt='%a, %d %b %Y %H:%M:%S',
filemode='w')######### ########################################################### ########################################定義一個StreamHandler,將INFO等級或更高的日誌資訊列印到標準錯誤,並將其新增至目前的日誌處理物件#console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: % (levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)################## ########################################################### #############################
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
螢幕上列印:
root 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] IN This is message
Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message
4.logging之日誌回滾
import loggingfrom 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.setFormatter(formatter)
logging.getLogger('').addHandler(Rdler )################################################# ###############################################
從上例和本例可以看出,logging有一個日誌處理的主對象,其它處理方式都是透過addHandler加入的。
由於StreamHandler和FileHandler是常用的日誌處理方式,所以直接包含在logging模組中,而其他方式則包含在logging.handlers模組中,
#logger.conf
############################### ################ [loggers]
keys=root,example01,example02
[logger_root]
level=DEBUG
handlers=hand01,hand02
level=DEBUG
handlers=hand01,hand02
level=DEBUG
handlers=hand01,hand02
hand[1] hand01,hand02
qualname=example01
propagate=0
[logger_example02]
handlers=hand01,hand03
qualname=example02##propagate=###
############################ [handlers]
keys=hand01,hand02,hand03
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stderr,)
[handler_hand02]class=FileHandler
level=DEBUG
formatter=form01
args=('myappargs>( 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=
import loggingimport logging.config
datefmt=
import loggingimport logging.config
datefmt=
import loggingimport logging.config
datefmt=
import loggingimport logging.config
datefmt=
importfile""
logger = logging.getLogger("example01")
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warningin日誌。 info message')
logger.warning('This is warning message')