# -*- coding: utf-8 -*-
import gzip
import shutil
from os import listdir, remove
from os.path import dirname, basename, splitext, join, exists
from logging.handlers import TimedRotatingFileHandler
import logging
# https://medium.com/@rahulraghu94/overriding-pythons-timedrotatingfilehandler-to-compress-your-log-files-iot-c766a4ace240
class TimedRotatingFileHandler(TimedRotatingFileHandler):
def __init__(self, filename="", when="midnight", interval=1, encoding="utf-8", backupCount=14):
super(TimedRotatingFileHandler, self).__init__(
filename=filename,
when=when,
interval=int(interval),
encoding=encoding,
backupCount=int(backupCount)
)
def doRollover(self):
super(TimedRotatingFileHandler, self).doRollover()
log_dir = dirname(self.baseFilename)
to_compress = [
join(log_dir, f) for f in listdir(log_dir) if f.startswith(
basename(splitext(self.baseFilename)[0])
) and not f.endswith((".gz", ".log"))
]
for f in to_compress:
if exists(f):
with open(f, "rb") as _old, gzip.open(f + ".gz", "wb") as _new:
shutil.copyfileobj(_old, _new)
remove(f)
def _setup_logging(logger_name, file_name, level=logging.INFO, when='midnight', interval=1, encoding='utf-8'):
_LOG = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s - [%(filename)s:%(lineno)s] - %(levelname)s - %(message)s')
file_handler = TimedRotatingFileHandler(filename=file_name, when=when, interval=interval, encoding=encoding)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
_LOG.setLevel(level)
_LOG.addHandler(file_handler)
_LOG.addHandler(stream_handler)
https://medium.com/@rahulraghu94/overriding-pythons-timedrotatingfilehandler-to-compress-your-log-files-iot-c766a4ace240