python的logging库中TimedRotatingFileHandler类问题

2023-11-20 22:38

本文主要是介绍python的logging库中TimedRotatingFileHandler类问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文网址:http://www.5dcode.com/?p=545

第一次用python,第一次用logging,第一次用TimedRotatingFileHandler,居然发现了其中的BUG,记录下吧。

      用TimedRotatingFileHandler的目的是让其自动在日志文件名后面加上日期时间,可以按秒、分、时、天、周或者其倍数来设置,BUG出现的场景是:手动设置时间,并把时间往未来时间调(比如把2012-03-15调成2014-03-15),这时就出问题了,这时产生每条日志后会产生一个日志文件,这并不是我们想要的效果,如果把当前时间再往历史时间调(比如把2012-03-15调成2010-03-15),这时也会产生问题:所有产生的日志都会记录到一个没有日期后缀的文件,并不会按日期分类。如果时间是正确的并按正常的流程走并不会产生问题,所以想看看logging是怎么实现的,看了其源码:C:\Python25\Lib\logging\handlers.py,果然不出所料,它的设计是有问题的,根本不考虑手动调时间或者时间可能不对需要同步的情况:

  1. def shouldRollover(self, record):
  2.         """
  3.         Determine if rollover should occur
  4.  
  5.         record is not used, as we are just comparing times, but it is needed so
  6.         the method siguratures are the same
  7.         """
  8.         t = int(time.time())
  9.         if t >= self.rolloverAt:
  10.             return 1
  11.         #print "No need to rollover: %d, %d" % (t, self.rolloverAt)
  12.         return 0
  13.  
  14.     def doRollover(self):
  15.         """
  16.         do a rollover; in this case, a date/time stamp is appended to the filename
  17.         when the rollover happens.  However, you want the file to be named for the
  18.         start of the interval, not the current time.  If there is a backup count,
  19.         then we have to get a list of matching filenames, sort them and remove
  20.         the one with the oldest suffix.
  21.         """
  22.         self.stream.close()
  23.         # get the time that this sequence started at and make it a TimeTuple
  24.         t = self.rolloverAt - self.interval
  25.         timeTuple = time.localtime(t)
  26.         dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
  27.         if os.path.exists(dfn):
  28.             os.remove(dfn)
  29.         os.rename(self.baseFilename, dfn)
  30.         if self.backupCount > 0:
  31.             # find the oldest log file and delete it
  32.             s = glob.glob(self.baseFilename + ".20*")
  33.             if len(s) > self.backupCount:
  34.                 s.sort()
  35.                 os.remove(s[0])
  36.         #print "%s -> %s" % (self.baseFilename, dfn)
  37.         if self.encoding:
  38.             self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
  39.         else:
  40.             self.stream = open(self.baseFilename, 'w')
  41.         self.rolloverAt = self.rolloverAt + self.interval

第9行判断中只判断时间大的情况,并没有判断时间小的情况,第41行中self.rolloverAt永远是上一次的值加上self.interval,如果时间往大的调的话,第9行判断就会永远是True,所以就会产生问题,如果时间往小的调,第9行判断就会永远是False,也会产生问题。上面的python版本是2.5.4,再看最新的版本:3.1,这个版本修复了def doRollover(self),但并没有修复def shouldRollover(self, record),所以综合这两个版本的考虑,还是自己来实现吧,修复之后的代码如下(测试成功通过):

 

  1. def shouldRollover(self, record):
  2.         """
  3.         Determine if rollover should occur
  4.  
  5.         record is not used, as we are just comparing times, but it is needed so
  6.         the method siguratures are the same
  7.         """
  8.         t = int(time.time())
  9.         #print "self.rolloverAt: %d. currentTime: %d." % (self.rolloverAt, t)
  10.         #start: recompare slef.rolloverAt, Modify by 5dcode. 2012-03-14 
  11.         if t >= self.rolloverAt or t < (self.rolloverAt - self.interval):
  12.             return 1
  13.         #end: recompare slef.rolloverAt, Modify by 5dcode. 2012-03-14 
  14.         #print "No need to rollover: %d, %d" % (t, self.rolloverAt)
  15.         return 0
  16.  
  17.     def doRollover(self):
  18.         """
  19.         do a rollover; in this case, a date/time stamp is appended to the filename
  20.         when the rollover happens.  However, you want the file to be named for the
  21.         start of the interval, not the current time.  If there is a backup count,
  22.         then we have to get a list of matching filenames, sort them and remove
  23.         the one with the oldest suffix.
  24.         """
  25.         self.stream.close()
  26.         # get the time that this sequence started at and make it a TimeTuple
  27.         t = self.rolloverAt - self.interval
  28.         timeTuple = time.localtime(t)
  29.         dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
  30.         if os.path.exists(dfn):
  31.             os.remove(dfn)
  32.         os.rename(self.baseFilename, dfn)
  33.         if self.backupCount > 0:
  34.             # find the oldest log file and delete it
  35.             s = glob.glob(self.baseFilename + ".20*")
  36.             if len(s) > self.backupCount:
  37.                 s.sort()
  38.                 os.remove(s[0])
  39.         #print "%s -> %s" % (self.baseFilename, dfn)
  40.         if self.encoding:
  41.             self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
  42.         else:
  43.             self.stream = open(self.baseFilename, 'w')
  44.         
  45.         #start: recompute self.rolloverAt, Modify by 5dcode. 2012-03-14 
  46.         currentTime = int(time.time())
  47.         newRolloverAt = currentTime + self.interval
  48.         if self.when == 'MIDNIGHT' or self.when.startswith('W'):
  49.             # This could be done with less code, but I wanted it to be clear
  50.             t = time.localtime(currentTime)
  51.             currentHour = t[3]
  52.             currentMinute = t[4]
  53.             currentSecond = t[5]
  54.             # r is the number of seconds left between now and midnight
  55.             r = _MIDNIGHT - ((currentHour * 60 + currentMinute) * 60 +
  56.                     currentSecond)
  57.             newRolloverAt = currentTime + r
  58.         #newRolloverAt = self.computeRollover(currentTime)
  59.         while newRolloverAt <= currentTime:
  60.             newRolloverAt = newRolloverAt + self.interval
  61.         #If DST changes and midnight or weekly rollover, adjust for this.
  62.         if self.when == 'MIDNIGHT' or self.when.startswith('W'):
  63.             dstNow = time.localtime(currentTime)[-1]
  64.             dstAtRollover = time.localtime(newRolloverAt)[-1]
  65.             if dstNow != dstAtRollover:
  66.                 if not dstNow:  # DST kicks in before next rollover, so we need to deduct an hour
  67.                     newRolloverAt = newRolloverAt - 3600
  68.                 else:           # DST bows out before next rollover, so we need to add an hour
  69.                     newRolloverAt = newRolloverAt + 3600
  70.         self.rolloverAt = newRolloverAt
  71.         #print "self.rolloverAt: %d." % self.rolloverAt
  72.         #end: recompute self.rolloverAt, Modify by 5dcode. 2012-03-14

 

 

 

 

这篇关于python的logging库中TimedRotatingFileHandler类问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/397941

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3