I'm using standart logger library in Python. There are RotatingFileHandler, that can rotate log files dayily, for example.
But it just renames them. Will be great, if it can not only rename, but also put old files in zip (or gz, bzip, etc) archive.
Is there easy way to achieve this?
I think your best option is to extend RotatingFileHandler something like this (not tested):
import os
from logging.handlers import RotatingFileHandler
COMPRESSION_SUPPORTED = {}
try:
import gzip
COMPRESSION_SUPPORTED['gz'] = gzip
except ImportError:
pass
try:
import zipfile
COMPRESSION_SUPPORTED['zip'] = zipfile
except ImportError:
pass
class NewRotatingFileHandler(RotatingFileHandler):
def __init__(self, *args, **kws):
compress_mode = kws.pop('compress_mode')
try:
self.compress_cls = COMPRESSION_SUPPORTED[compress_mode]
except KeyError:
raise ValueError('"%s" compression method not supported.' % compress_mode)
super(NewRotatingFileHandler, self).__init__(self, *args, **kws)
def doRollover(self):
super(NewRotatingFileHandler, self).doRollover()
# Compress the old log.
old_log = self.baseFilename + ".1"
with open(old_log) as log:
with self.compress_cls.open(old_log + '.gz', 'wb') as comp_log:
comp_log.writelines(log)
os.remove(old_log)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With