python修改logging模块实现日志按天写入
来源:互联网 发布:人工智能股票代码 编辑:程序博客网 时间:2024/05/01 10:18
最近使用python开发自动化测试后台,日志模块利用配置文件建立,如下:
#log4py.properties###############################################[loggers]keys=root[logger_root]level=DEBUGhandlers=hand01,hand02###############################################[handlers]keys=hand01,hand02[handler_hand01]class=StreamHandlerlevel=DEBUGformatter=form02args=(sys.stdout,)[handler_hand02]class=handlers.TimedRotatingFileHandlerlevel=DEBUGformatter=form01args=('../log/auto_test.log', 'D',1,0)###############################################[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)sdatefmt=%a, %d %b %Y %H:%M:%S[formatter_form02]#format=%(name)-12s: %(levelname)-8s %(message)s#format=%(message)sdatefmt=
使用今天后发现并没有像想象中的那样每天新建一个log文件,并把以前的文件按天进行重命名,查看了logging模块源码(Python-2.7.8/Lib/logging/handlers.py)后发现问题所在,代码如下:
def shouldRollover(self, record): """ Determine if rollover should occur. record is not used, as we are just comparing times, but it is needed so the method signatures are the same """ t = int(time.time()) if t >= self.rolloverAt: return 1 #print "No need to rollover: %d, %d" % (t, self.rolloverAt) return 0
其中self.rolloverAt是log文件最后一次修改的时间+配置文件设置的时间间隔interval(如上则是60 * 60 * 24),那么根据程序判断配置的时间间隔应该就是离上次修改的时间,只有在这时才会重命名旧的日志文件并新建新的日志文件,既不是按照日志文件创建时间(猜测是由于unix不记录文件创建时间),也不是按照每天或者每周等方式重命名旧的日志文件并新建新的日志文件,因此通过修改上面的源代码就可以实现按照每天或者每分钟等方式重命名旧的日志文件并新建新的日志文件,代码如下:
def shouldRollover(self, record): """ Determine if rollover should occur. record is not used, as we are just comparing times, but it is needed so the method signatures are the same """ t = int(time.time()) #此处只实现按分钟和按天重建,其他方式可据此修改 if self.when.startswith('M') and time.localtime(t).tm_min==time.localtime(self.rolloverAt).tm_min: return 1 if self.when.startswith('D') and time.localtime(t).tm_mday==time.localtime(self.rolloverAt).tm_mday: return 1 if t >= self.rolloverAt: return 1 #print "No need to rollover: %d, %d" % (t, self.rolloverAt) return 0
0 0
- python修改logging模块实现日志按天写入
- Python日志模块-logging
- Python日志模块logging
- Python 日志(logging)模块
- Python:logging日志模块
- python日志模块----logging
- python 日志模块 logging
- python(logging )日志模块
- python logging日志模块
- Python 日志logging模块
- python logging日志模块
- python日志模块---logging
- python 日志模块logging
- Python--日志模块Logging
- python logging日志模块
- Python logging 日志模块
- python日志模块(logging)
- python-logging.config【日志模块】
- 支持向量机通俗导论(理解SVM的三层境界)
- 数据挖掘入门
- Java中byte与16进制字符串的互相转换
- Hibernate注释下的自定义架构实现
- Hadoop-2.2.0 wordcount 编译打包执行 (java.lang.ClassNotFoundException)
- python修改logging模块实现日志按天写入
- GridView 按钮
- SpringMVC之实现用户管理
- UVA 1342 That Nice Euler Circuit(二维几何基础)
- CodeForces-71A-Way Too Long Words
- 字符串操作
- MySQL字符串函数
- Android学习之GroupActivity
- BZOJ 1012 JSOI2008 最大数maxnumber 单调栈+二分 / 线段树