Last active
January 5, 2021 19:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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