python中的logger之二

Rotating-logger

日志文件太大,一般不容易使用。现在的日志系统一般都提供了方便的日志回绕分片。一般有按照文件大小、记录时间长度来对日志文件分片。
在python logging中,提供了这2种分片方式。

  1. 按照文件大小分片

这种方式使用的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

  1. 按照时间长度分片

这里使用的是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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值