Note that there are some explanatory texts on larger screens.

plurals
  1. POThe logging.handlers: How to rollover after time or maxBytes?
    primarykey
    data
    text
    <p>I do struggle with the logging a bit. I'd like to roll over the logs after certain period of time and also after reaching certain size.</p> <p>Rollover after a period of time is made by <a href="http://docs.python.org/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler" rel="noreferrer"><code>TimedRotatingFileHandler</code></a>, and rollover after reaching certain log size is made by <a href="http://docs.python.org/library/logging.handlers.html#rotatingfilehandler" rel="noreferrer"><code>RotatingFileHandler</code></a>.</p> <p>But the <code>TimedRotatingFileHandler</code> doesn't have the attribute <code>maxBytes</code> and the <code>RotatingFileHandler</code> can not rotate after a certain period of time. I also tried to add both handlers to logger, but the result was doubled logging.</p> <p>Do I miss something?</p> <p>I also looked into source code of <code>logging.handlers</code>. I tried to subclass <code>TimedRotatingFileHandler</code> and override the method <code>shouldRollover()</code> to create a class with capabilities of both:</p> <pre><code>class EnhancedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler): def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=0, maxBytes=0): """ This is just a combination of TimedRotatingFileHandler and RotatingFileHandler (adds maxBytes to TimedRotatingFileHandler) """ # super(self). #It's old style class, so super doesn't work. logging.handlers.TimedRotatingFileHandler.__init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=0) self.maxBytes=maxBytes def shouldRollover(self, record): """ Determine if rollover should occur. Basically, see if the supplied record would cause the file to exceed the size limit we have. we are also comparing times """ if self.stream is None: # delay was set... self.stream = self._open() if self.maxBytes &gt; 0: # are we rolling over? msg = "%s\n" % self.format(record) self.stream.seek(0, 2) #due to non-posix-compliant Windows feature if self.stream.tell() + len(msg) &gt;= self.maxBytes: return 1 t = int(time.time()) if t &gt;= self.rolloverAt: return 1 #print "No need to rollover: %d, %d" % (t, self.rolloverAt) return 0 </code></pre> <p>But like this the log creates one backup and the gets overwritten. Seems like I have to override also method <code>doRollover()</code> which is not so easy.</p> <p>Any other idea how to create a logger which rolls the file over after certain time and also after certain size reached?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload