Friday 3 September 2010

Using a custom file naming scheme for rotated log files

When you use rotated log files with Python logging's built-in functionality, the rotated files are named by appending a number to the base log file name: thus, app.log would give rise to log files app.log, app.log.1, app.log.2 etc.

Sometimes, you may not want this: for example, you may want to preserve the file extension so that you can take advantage of file associations (typically on Windows). You can implement a scheme where log file names take the form app.log, app.1.log, app.2.log etc. by subclassing RotatingFileHandler and overriding the doRollover() method. The exact code for this method varies slightly across different versions of Python, so I won't reproduce the whole method, but there's always an if statement in the method which does the rotation. In your overridden method, you can use the following logic (in place of the default logic) to implement an extension-preserving rotation scheme, for example as follows:

if self.backupCount > 0:
    name, ext = os.path.splitext(self.baseFilename)
    for i in range(self.backupCount - 1, 0, -1):
        sfn = "%s.%d%s" % (name, i, ext)
        dfn = "%s.%d%s" % (name, i + 1, ext)
        if os.path.exists(sfn):
            if os.path.exists(dfn):
                os.remove(dfn)
            os.rename(sfn, dfn)
    dfn = "%s.1%s" % (name, ext)
    if os.path.exists(dfn):
        os.remove(dfn)
    os.rename(self.baseFilename, dfn)

1 comment:

  1. Its sad that the format of the name of rolled files is hard-coded in build-in library.

    ReplyDelete