Rotating-logger
日志文件太大,一般不容易使用。现在的日志系统一般都提供了方便的日志回绕分片。一般有按照文件大小、记录时间长度来对日志文件分片。
在python logging中,提供了这2种分片方式。
- 按照文件大小分片
这种方式使用的Handler是RotatingFileHandler;
class RotatingFileHandler(BaseRotatingHandler):
"""
Handler for logging to a set of files, which switches from one file
to the next when the current file reaches a certain size.
"""
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
下面看看使用方式:
rlogger = logging.getLogger("loggingtest2")
rfh = RotatingFileHandler("log/rtest.log", maxBytes=2000,backupCount=3)
formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S')
rfh.setFormatter(formatter)
rlogger.addHandler(rfh)
完整示例:
import sys,time
import logging
from logging.handlers import RotatingFileHandler,TimedRotatingFileHandler
rlogger = logging.getLogger("loggingtest2")
rfh = RotatingFileHandler("log/rtest.log", maxBytes=2000,backupCount=3)
formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S')
rfh.setFormatter(formatter)
rlogger.addHandler(rfh)
rlogger.setLevel(level = logging.INFO)
i = 0
while i<2000:
rlogger.info("this is a %s %i.......","info",i)
rlogger.warning("this is a %s %i........", "warning",i)
i=i+1
执行时,会在log下生成rtest.log,以及分片的3个backup文件rtest.log.0, rtest.log.1, rtest.log.2
- 按照时间长度分片
这里使用的是TimedRotatingFileHandler。
定义如下:
class TimedRotatingFileHandler(BaseRotatingHandler):
"""
Handler for logging to a file, rotating the log file at certain timed
intervals.
If backupCount is > 0, when rollover is done, no more than backupCount
files are kept - the oldest ones are deleted.
"""
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
使用方式:
formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S')
trlogger = logging.getLogger()
trfh = TimedRotatingFileHandler("log/tr_test.log",when="S",interval=5,backupCount=3)
trfh.setFormatter(formatter)
trlogger.addHandler(trfh)
完整示例:
import sys,time
import logging
from logging.handlers import RotatingFileHandler,TimedRotatingFileHandler
formatter = logging.Formatter('%(name)-12s %(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S')
trlogger = logging.getLogger()
trfh = TimedRotatingFileHandler("log/tr_test.log",when="S",interval=5,backupCount=3)
trfh.setFormatter(formatter)
trlogger.addHandler(trfh)
trlogger.setLevel(level = logging.INFO)
i=0
while i < 500:
trlogger.info("this is a %s .......","info")
trlogger.warning("this is a %s........", "warning")
time.sleep(0.5)
i=i+1