我在下面提到了使用旋转器和命名器功能的链接,但它们不起作用(没有任何区别)
我希望将日志压缩并命名为 system1.log.gz,但它们像 system.log.1 一样保存,因此我进行了以下更改,但仍然无法正常工作。
环境: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)
预期的压缩日志名称:system1.log.gz
实际非压缩日志名称:system.log.1