Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compact (archive) old log files in python

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?

like image 303
werewindle Avatar asked Aug 10 '11 11:08

werewindle


1 Answers

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)
like image 89
mouad Avatar answered Sep 22 '22 01:09

mouad