Naming rotating log files specific name in Python
<p>I want to name rotating log files as I want.</p>

<p>For example, if I use RotatingFileHandler, It separates log file when it reaches to specific file size naming <code>log file name + extension numbering</code>, like below.</p>

<pre><code>filename.log      #first log file
filename.log.1    #rotating log file1
filename.log.2    #rotating log file2
</code></pre>

<p>However, I want log handler to name them every time it is created at.
For example.</p>

<pre><code>09-01-12-20.log    #first log file
09-01-12-43.log    #rotating log file1
09-01-15-00.log    #rotating log file2
</code></pre>

<p>How can I do this ?</p>

<p>Edit---------------------------</p>

<p>I am not asking how to create and name a file.</p>

<p>I want to facailate python <code>logging</code> package doing something like inheriting and overriding <code>logging</code> .</p>

<p>I inherit and override <code>RotatingFileHandler</code> of python logging handler.</p>

<p>RotatingFileHandler has <code>self.baseFilename</code> value, the handler will use <code>self.baseFilename</code> to create logFile.(when it creates file first or when rollover happens)</p>

<p><code>self.shouldRollover()</code> method, It checks if the handler should rollover logfile or not.</p>

<p>If this method <code>return 1</code>, it means rollover should happen or <code>return 0</code>.</p>

<p>By overriding them, I define when this handler makes rollover and which name should be used for new log file by rollover.</p>

<p><em>-----------------------------------------Edit-----------------------------------------</em></p>

<p>I post the example code.</p>

<pre><code>from logging import handlers

class DailyRotatingFileHandler(handlers.RotatingFileHandler):

    def __init__(self, alias, basedir, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
        """
        @summary: 
        Set self.baseFilename to date string of today.
        The handler create logFile named self.baseFilename
        """
        self.basedir_ = basedir
        self.alias_ = alias

        self.baseFilename = self.getBaseFilename()

        handlers.RotatingFileHandler.__init__(self, self.baseFilename, mode, maxBytes, backupCount, encoding, delay)

    def getBaseFilename(self):
        """
        @summary: Return logFile name string formatted to "today.log.alias"
        """
        self.today_ = datetime.date.today()
        basename_ = self.today_.strftime("%Y-%m-%d") + ".log" + '.' + self.alias_
        return os.path.join(self.basedir_, basename_)

    def shouldRollover(self, record):
        """
        @summary: 
        Rollover happen 
        1. When the logFile size is get over maxBytes.
        2. When date is changed.

        @see: BaseRotatingHandler.emit
        """

        if self.stream is None:                
            self.stream = self._open()

        if self.maxBytes &gt; 0 :                  
            msg = "%s\n" % self.format(record)
            self.stream.seek(0, 2)  
            if self.stream.tell() + len(msg) &gt;= self.maxBytes:
                return 1

        if self.today_ != datetime.date.today():
            self.baseFilename = self.getBaseFilename()
            return 1

        return 0
</code></pre>

<p>This DailyRotatingFileHandler will create logfile like</p>

<pre><code>2016-10-05.log.alias
2016-10-05.log.alias.1
2016-10-05.log.alias.2
2016-10-06.log.alias
2016-10-06.log.alias.1
2016-10-07.log.alias.1
</code></pre>

python logging