1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it, I have done compression and naming both in doRollover only and it is working! Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.