Skip to content

Instantly share code, notes, and snippets.

@mzpqnxow
Last active January 5, 2021 19:57
Show Gist options
  • Save mzpqnxow/503b3aadd6a29931e00ff4cd7c8958df to your computer and use it in GitHub Desktop.
Save mzpqnxow/503b3aadd6a29931e00ff4cd7c8958df to your computer and use it in GitHub Desktop.
Python LzmaRotatingFileHandler: Simple extension of RotatingFileHandler that using LZMA compression after rolling a log file
import logging.handlers
import lzma
import os
"""
This can be done a few different ways. You can also implement it as a standalone
function, instnatiate a standard logging.handlers.RotatingFileHandler, and then
assign your own rotator() function to to the `rotator` attribute of the instance
I think it's more convenient encapsulating it like this, though
-AG
"""
MAX_LOG_SIZE = 1 << 24 # 24MB
MAX_LOG_BACKUP = 10 # Keep the 10 most recent
class LzmaRotatingFileHandler(logging.handlers.RotatingFileHandler):
"""Rotating log handler that uses LZMA to compress when rolling over the log file
Usage is identical to logging.handlers.RotatingFileHandler:
file_handler = LzmaRotatingFileHandler(outfile, maxBytes=MAX_LOG_SIZE, backupCount=MAX_LOG_BACKUP)
file_handler.setLevel(file_log_level)
file_formatter = logging.Formatter('%(levelname)s %(funcName)s():%(lineno)d\t%(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
"""
def __init__(self, *args, **kwargs):
super(LzmaRotatingFileHandler, self).__init__(*args, **kwargs)
self.rotator = self._rotator
@staticmethod
def _rotator(source, dest):
os.rename(source, dest)
with open(dest, 'rb') as infd, lzma.open("%s.lzma" % dest, mode='wb') as outfd:
outfd.writelines(infd)
os.remove(dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment