扩展TimedRotatingFileHandler实现压缩
函数列表
getFilesToDelete
调用getFilesToDelete,获取要删除log文件列表
getFilesToDelete
根据self.baseFilename和self.suffix,获取已保留的log文件
doRollover
当出现log转存时调用,解决转存时压缩问题
doGzip
对文件进行压缩
实现代码
代码示例
import logging from logging.handlers import TimedRotatingFileHandler import gzip import time class GzTimedRotatingFileHandler(TimedRotatingFileHandler): def __init__(self, filename, when, backupCount, interval, delay=False): super(GzTimedRotatingFileHandler, self).__init__(filename, when, interval, backupCount, delay=delay) def doGzip(self, old_log): with open(old_log) as old: with gzip.open(old_log + '.gz', 'wb') as comp_log: comp_log.writelines(old) os.remove(old_log) def getFilesToDelete(self): """ Determine the files to delete when rolling over. More specific than the earlier method, which just used glob.glob(). """ dirName, baseName = os.path.split(self.baseFilename) fileNames = os.listdir(dirName) result = [] prefix = baseName + "." plen = len(prefix) for fileName in fileNames: if fileName[:plen] == prefix: suffix = fileName[plen:].rstrip(".gz") if self.extMatch.match(suffix): result.append(os.path.join(dirName, fileName)) result.sort() if len(result) < self.backupCount: result = [] else: result = result[:len(result) - self.backupCount] return result def doRollover(self): if self.stream: self.stream.close() self.stream = None # get the time that this sequence started at and make it a TimeTuple currentTime = int(time.time()) dstNow = time.localtime(currentTime)[-1] t = self.rolloverAt - self.interval if self.utc: timeTuple = time.gmtime(t) else: timeTuple = time.localtime(t) dstThen = timeTuple[-1] if dstNow != dstThen: if dstNow: addend = 3600 else: addend = -3600 timeTuple = time.localtime(t + addend) dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple) if os.path.exists(dfn): os.remove(dfn) # Issue 18940: A file may not have been created if delay is True. if os.path.exists(self.baseFilename): os.rename(self.baseFilename, dfn) self.doGzip(dfn) if self.backupCount > 0: for s in self.getFilesToDelete(): os.remove(s) #if self.stream is None: self.stream = self._open() newRolloverAt = self.computeRollover(currentTime) while newRolloverAt <= currentTime: newRolloverAt = newRolloverAt + self.interval if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc: dstAtRollover = time.localtime(newRolloverAt)[-1] if dstNow != dstAtRollover: if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour addend = -3600 else: # DST bows out before next rollover, so we need to add an hour addend = 3600 newRolloverAt += addend self.rolloverAt = newRolloverAt
调用示例
def test_GzTimedRotatingFileHandler(): log_filename='/var/log/logTest/log_rotate' logger=logging.getLogger('MyLogger') logger.setLevel(logging.DEBUG) handler = GzTimedRotatingFileHandler(filename=log_filename, maxBytes=10, when="D", backupCount=5, interval=1) logger.addHandler(handler) for i in range(100000): time.sleep(0.5) logger.debug('i=%d ' % i) if __name__=="__mian__": test_GzTimedRotatingFileHandler()