codekingpro/portable-devtools
114k
1##########################################################################2#3# pgAdmin 4 - PostgreSQL Tools4#5# Copyright (C) 2013 - 2024, The pgAdmin Development Team6# This software is released under the PostgreSQL Licence7#8##########################################################################9 10import re11from logging import handlers12 13 14class EnhancedRotatingFileHandler(handlers.TimedRotatingFileHandler,15 handlers.RotatingFileHandler):16 """17 Handler for logging to a set of files, which switches from one file18 to the next when the current file reaches a certain size, or at certain19 timed intervals20 @filename - log file name21 @max_bytes - file size in bytes to rotate file22 @interval - Duration to rotate file23 @backup_count - Maximum number of files to retain24 @encoding - file encoding25 @when - 'when' events supported:26 # S - Seconds27 # M - Minutes28 # H - Hours29 # D - Days30 # midnight - roll over at midnight31 # W{0-6} - roll over on a certain day; 0 - Monday32 Here we are defaulting rotation with minutes interval33 """34 def __init__(self, filename, max_bytes=1, interval=60, backup_count=0,35 encoding=None, when='M'):36 max_bytes = max_bytes * 1024 * 102437 handlers.TimedRotatingFileHandler.__init__(self, filename=filename,38 when=when,39 interval=interval,40 backupCount=backup_count,41 encoding=encoding)42 43 handlers.RotatingFileHandler.__init__(self, filename=filename,44 mode='a',45 maxBytes=max_bytes,46 backupCount=backup_count,47 encoding=encoding)48 49 # Time & Size combined rollover50 def shouldRollover(self, record):51 return handlers.TimedRotatingFileHandler.shouldRollover(self, record) \52 or handlers.RotatingFileHandler.shouldRollover(self, record)53 54 # Roll overs current file55 def doRollover(self):56 self.suffix = "%Y-%m-%d_%H-%M-%S"57 self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$"58 self.extMatch = re.compile(self.extMatch, re.ASCII)59 handlers.TimedRotatingFileHandler.doRollover(self)60 