TimedRotatingFileHandler日志的文件名约定? (蟒蛇)

时间:2015-01-20 15:34:58

标签: python

Python 2.7: 每次日志遇到翻转事件(使用RotatingFileHandler记录日志)时,都会进行“备份”操作。生成日志。

例如:

logFile = 'general.log'
file_handler = logging.handlers.TimedRotatingFileHandler(logFile,when="midnight")

在午夜翻转和翻转事件中的结果创建了以下文件:

general.log.2015-01-21

此模块是否为这些文件名的结构提供了任何灵活性? 即使用不同的约定...... 20150121_general.log

1 个答案:

答案 0 :(得分:7)

简短回答是否定的:根据TimedRotatingFileHandler文档,你无法做到。

后缀更改基于when参数,您可以在代码https://hg.python.org/cpython/file/2.7/Lib/logging/handlers.py#l187中看到

从相同的源代码中您可以看到覆盖suffix很简单,但您也必须覆盖extMatch

class MyTimedRotatingFileHandler(TimedRotatingFileHandler):
    def __init__(self, *args, **kwargs):
        super(MyTimedRotatingFileHandler,self).__init__(*args,**kwargs)
        self.suffix = "%Y%m%d"
        self.extMatch = re.compile(r"^\d{4}\d{2}\d{2}$")

不幸的是,替换点分隔符并交换suffixbasename并不是那么简单,您必须重写doRollover()getFilesToDelete()方法。

黑客攻击可能是这样的(未经测试)...我希望它有效,但我不能给出任何保证:)

class MyTimedRotatingFileHandler(TimedRotatingFileHandler):

        self.extMatch = r"^\d{4}-\d{2}-\d{2}$"

   def getFilesToDelete(self):
        """ CUT, PASTE AND .... HACK
        """
        dirName, baseName = os.path.split(self.baseFilename)
        fileNames = os.listdir(dirName)
        result = []
        extMatch = re.compile(r"^\d{4}\d{2}\d{2}$")
        ends = "_" + baseName + ".log"
        elen = len(ends)
        for fileName in fileNames:
            if fileName[-elen:] == ends:
                date = fileName[-elen:]
                if self.extMatch.match(date):
                    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):
        """
        CUT AND PAST FROM TimedRotatingFileHandler
        customize file name by prefix instead suffix
        """
        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)
        #################################################
        # THE HACK!!!! ##################################
        ##################################################
        dfn = time.strftime("%Y%m%d", timeTuple) + "_" +self.baseFilename + ".log"
        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)
        if self.backupCount > 0:
            for s in self.getFilesToDelete():
                os.remove(s)
        if not self.delay:
            self.stream = self._open()
        newRolloverAt = self.computeRollover(currentTime)
        while newRolloverAt <= currentTime:
            newRolloverAt = newRolloverAt + self.interval
        #If DST changes and midnight or weekly rollover, adjust for this.
        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