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?
You can automatically write bz2 compressed log files by initializing the
RotatingFileHandler
withencoding='bz2-codec'
:PS. Python3 removed 'bz2-codec' from the set of valid encodings, so this solution is specific to Python2.
I think your best option is to extend RotatingFileHandler something like this (not tested):
The accepted answer will archive only 1 file-(basefile.log.1) .The other files are not archived. This code will archive all the log files other than the base file.