correction de la classe de rotation des logs soit en fonction du temps, soit en fonction de la taille

This commit is contained in:
2022-12-21 20:52:33 +01:00
parent fd79a53348
commit 0ccae34305

View File

@@ -32,32 +32,58 @@ class SizedTimedRotatingFileHandler(handlers.TimedRotatingFileHandler):
to the next when the current file reaches a certain size, or at certain
timed intervals
"""
def __init__(self, filename, maxBytes=0, backupCount=0, encoding=None,
def __init__(self, filename, maxBytes=0, mode='a', backupCount=0, encoding=None,
delay=0, when='h', interval=1, utc=False):
handlers.TimedRotatingFileHandler.__init__(self, filename, when, interval,
backupCount, encoding, delay, utc)
self.maxBytes = maxBytes
handlers.TimedRotatingFileHandler.__init__(self,
filename=filename,
when=when,
interval=interval,
backupCount=backupCount,
encoding=encoding,
delay=delay,
utc=utc)
handlers.RotatingFileHandler.__init__(self,
filename=filename,
mode=mode,
maxBytes=maxBytes,
backupCount=backupCount,
encoding=encoding,
delay=delay)
def computeRollover(self, current_time):
'''
'''
return handlers.TimedRotatingFileHandler.computeRollover(self, current_time)
def shouldRollover(self, record):
"""
Determine if rollover should occur.
'''
'''
return handlers.TimedRotatingFileHandler.shouldRollover(self, record) or handlers.RotatingFileHa
ndler.shouldRollover(self, record)
Basically, see if the supplied record would cause the file to exceed
the size limit we have.
"""
if self.stream is None: # delay was set...
self.stream = self._open()
if self.maxBytes > 0: # are we rolling over?
msg = "%s\n" % self.format(record)
# due to non-posix-compliant Windows feature
self.stream.seek(0, 2)
if self.stream.tell() + len(msg) >= self.maxBytes:
return 1
t = int(time.time())
if t >= self.rolloverAt:
return 1
return 0
def doRollover(self):
'''
'''
current_time = int(time.time())
dst_now = time.localtime(current_time)[-1]
new_rollover_at = self.computeRollover(current_time)
while new_rollover_at <= current_time:
new_rollover_at = new_rollover_at + 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:
dst_at_rollover = time.localtime(new_rollover_at)[-1]
if dst_now != dst_at_rollover:
if not dst_now: # 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
new_rollover_at += addend
self.rolloverAt = new_rollover_at
return handlers.RotatingFileHandler.doRollover(self)
def validate_json(json_data={}, logger=None):
@@ -786,7 +812,7 @@ def main():
fl.setFormatter(formatter)
logger.addHandler(fl)
handler = SizedTimedRotatingFileHandler(os.path.join('/var/log/kineintercom','Intercom.log'),
maxBytes=10000000, backupCount=30, when='d', interval=1)
maxBytes=1048576, backupCount=30, when='D', interval=1)
handler.setLevel(log.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)