python-logging模塊使用介紹

1.通過logging.basicConfig函數對日誌的輸出格式及方式做相關配置

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='example.log',
                    filemode='a')

logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')

example.log文件:
Thu, 14 Mar 2019 09:56:00 test111.py[line:11] INFO This is info message
Thu, 14 Mar 2019 09:56:00 test111.py[line:12] WARNING This is warning message


2.通過時間對日誌進行切割

import logging
from logging.handlers import TimedRotatingFileHandler

#創建logger,應用程序日誌輸入的接口
logger1=logging.getLogger()
logger1.setLevel(logging.INFO)             #設置logger日誌級別,允許輸入的日誌級別

#創建handler,日誌輸出的接口
handler1 = logging.handlers.TimedRotatingFileHandler("test.log", 'M', 1, 0)        #定義一個1分鐘換一次log文件的handler
handler1.suffix = "%Y%m%d-%H%M.log"        #設置日誌文件的後綴,跟strftime的格式一樣
handler1.setLevel(logging.INFO)            #設置handler日誌級別,輸出的日誌級別

#創建formatter,日誌格式
formatter1 = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')

logger1.addHandler(handler1)              #給logger1添加handler
handler1.setFormatter(formatter1)         #給handler1添加formatter

logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')


#TimedRotatingFileHandler介紹,可以將日誌按照時間切分
#TimedRotatingFileHandler(filename [,when [,interval [,backupCount]]])

# filename 是輸出日誌文件名的前綴
# when 是一個字符串的定義如下:
# “S”: Seconds
# “M”: Minutes
# “H”: Hours
# “D”: Days
# “W”: Week day (0=Monday)
# “midnight”: Roll over at midnight
# interval 是指等待多少個單位when的時間後,Logger會自動重建文件,當然,這個文件的創建
# 取決於filename+suffix,若這個文件跟之前的文件有重名,則會自動覆蓋掉以前的文件,所以
# 有些情況suffix要定義的不能因爲when而重複。
# backupCount 是保留日誌個數。默認的0是不會自動刪除掉日誌。若設10,則在文件的創建過程中
# 庫會判斷是否有超過這個10,若超過,則會從最先創建的開始刪除。

3.通過配置文件方式配置日誌

(1).logger.conf文件

#logger.conf
###############################################
[loggers]
keys=root,example01,example02
[logger_root]
level=DEBUG
handlers=hand01,hand02
[logger_example01]
handlers=hand01,hand02
qualname=example01
propagate=0                    #是否繼承父節點,0爲否,1爲是。
[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=DEBUG
formatter=form01
args=('myapp.log', 'a')
[handler_hand03]                #打印日誌到文件,並按照設置時間對日誌進行切割
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=form01
args=("test.log", 'S', 1, 0)
###############################################
[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=


(2).調用配置文件,打印日誌

import logging
import logging.config

logging.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 logging
# import logging.config
#
# logging.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')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章