快速实现Python多进程logging日志按天切割

        前几天填了一个前人留下的“日志”坑,程序日志只有程序重启才可以生成一个新日志文件,这样就会导致程序长时间运行之后,会生成一个巨大的日志文件,不方便后期问题查询或磁盘清理,所以日志按天切割势在必行。


python 原生logging日志模块可以满足大部分需求,但是唯独不适合多进程下日志切割,具体的原因可以自行百度一下,网上有很多讲解的,我就说一下,如何用最小的改动logging源码的方法来实现pyhon多进程下日志按天(分钟、小时)切割。

直接上代码:

1、修改logging模块中的TimedRotatingFileHandler(BaseRotatingHandler)类方法doRollover(self):

class TimedRotatingFileHandler(BaseRotatingHandler):

def doRollover(self):
        """
        do a rollover; in this case, a date/time stamp is appended to the filename
        when the rollover happens.  However, you want the file to be named for the
        start of the interval, not the current time.  If there is a backup count,
        then we have to get a list of matching filenames, sort them and remove
        the one with the oldest suffix.
        """
        if s

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值