Skip to content

Commit

Permalink
v1.1.2.42 - Fixed bugs and add deflated compression to compressed_siz…
Browse files Browse the repository at this point in the history
…ed_time_rotating_logger; renamed sys_logge to msg_logger and reorganized loggers initialisation
  • Loading branch information
kaufmanno committed Jan 19, 2018
1 parent a06f5f6 commit 559066e
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 223 deletions.
21 changes: 9 additions & 12 deletions ardas/compressed_sized_timed_rotating_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ class CompressedSizedTimedRotatingFileHandler(handlers.TimedRotatingFileHandler)
to the next when the current file reaches a certain size, or at certain
timed intervals
"""
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None,
delay=0, when='h', interval=1, utc=False):
# If rotation/rollover is wanted, it doesn't make sense to use another
# mode. If for example 'w' were specified, then if there were multiple
# runs of the calling application, the logs from previous runs would be
# lost if the 'w' is respected, because the log file would be truncated
# on each run.
handlers.TimedRotatingFileHandler.__init__(self, filename, when, interval, backupCount, encoding, delay, utc)
self.maxBytes = maxBytes
def __init__(self, filename, max_bytes=0, backup_count=0, encoding=None,
delay=0, when='h', interval=1, utc=False, zip_mode=zipfile.ZIP_DEFLATED):
handlers.TimedRotatingFileHandler.__init__(self, filename=filename, when=when, interval=interval, utc=utc,
backupCount=backup_count, encoding=encoding, delay=delay)
self.maxBytes = max_bytes
self.zip_mode = zip_mode

def shouldRollover(self, record):
"""
Expand All @@ -31,7 +28,7 @@ def shouldRollover(self, record):
self.stream = self._open()
if self.maxBytes > 0: # are we rolling over?
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) # due to non-posix-compliant Windows feature
self.stream.seek(0, 2) # due to non-posix-compliant Windows feature
if self.stream.tell() + len(msg) >= self.maxBytes:
return True
t = int(time.time())
Expand All @@ -48,7 +45,7 @@ def find_last_rotated_file(self):
if file_name.startswith(prefix) and not file_name.endswith('.zip'):
result.append(file_name)
result.sort()
return os.path(dir_name, result[0])
return os.path.join(dir_name, result[0])

def doRollover(self):
super(CompressedSizedTimedRotatingFileHandler, self).doRollover()
Expand All @@ -57,7 +54,7 @@ def doRollover(self):
dfn_zipped = '{}.zip'.format(dfn)
if os.path.exists(dfn_zipped):
os.remove(dfn_zipped)
with zipfile.ZipFile(dfn_zipped, 'w') as f:
with zipfile.ZipFile(dfn_zipped, 'w', self.zip_mode) as f:
f.write(dfn)
print('zip ' + dfn)
os.remove(dfn)

0 comments on commit 559066e

Please sign in to comment.