Search code examples
pythonloggingzlibfilehandlerlog-rotation

Custom rotator and namer functions not working with RotatingFileHandler logger handler in python?


I have referred below link for using rotator and namer functions, but they are not working(not making any difference)

Link: https://docs.python.org/3/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing

I want the logs to be compressed and named like system1.log.gz, but they are saving like system.log.1, hence I made below changes, but still, it is not working.

Environment: python 2.7.5

import logging
import os
import zlib
from logging.handlers import RotatingFileHandler
LOG_PATH = "/tmp"
FILE_NAME = "system.log"

Logger = logging.getLogger()

def namer(name):
    orig_name = name.split(".")
    return orig_name[0] + orig_name[2] + ".log.gz"


def rotator(source, dest):
    with open(source, "rb") as sf:
        data = sf.read()
        compressed = zlib.compress(data, 9)
        with open(dest, "wb") as df:
            df.write(compressed)
    os.remove(source)

logFormatter = logging.Formatter(
    "%(asctime)s %(levelname)s [%(threadName)s] %(filename)s:%(lineno)d %(message)s")
Logger.setLevel(logging.DEBUG)

fileHandler = RotatingFileHandler(
"{0}/{1}".format(LOG_PATH, FILE_NAME), maxBytes=1000,    backupCount=10)
fileHandler.setFormatter(logFormatter)
fileHandler.rotator = rotator
fileHandler.namer = name
Logger.addHandler(fileHandler)

Expected compressed log name: system1.log.gz

Actual non-compressed log name: system.log.1


Solution

  • You are using the cookbook for python 3, but run your code with python 2.7.5. The feature you are trying to use simply does not exist in 3. The best solution is to use python 3 as version 2 will retire in a few months. If that is not an option you can achieve the desired behaviour by creating your own handler class that inherits from RotatingFileHandler and overwrites the emit and doRollover. emit needs to do the compression, and doRollover the naming.