Note. On Unix systems, you should seriously consider using an external program written for this exact task:
logrotate (rotates, compresses and writes system logs)
You can set the speed so high that the first file will be deleted after 100 years or so.
In Python 2, logging.FileHandler accepts the encoding keyword argument, which can be set to bz2 or zlib .
This is because logging uses the codecs module, which in turn treats bz2 (or zlib ) as encoding:
>>> import codecs >>> with codecs.open("on-the-fly-compressed.txt.bz2", "w", "bz2") as fh: ... fh.write("Hello World\n") $ bzcat on-the-fly-compressed.txt.bz2 Hello World
Python version 3 (although docs mention bz2 as an alias, you really need to use bz2_codec - at least from 3.2.3):
>>> import codecs >>> with codecs.open("on-the-fly-compressed.txt.bz2", "w", "bz2_codec") as fh: ... fh.write(b"Hello World\n") $ bzcat on-the-fly-compressed.txt.bz2 Hello World
source share