Skip to main content
 首页 » 编程设计

python之轮换后如何压缩每日日志文件

2025年05月04日8langtianya

我目前在为我的应用程序管理旋转日志文件时遇到了麻烦,但是日志变得越来越大,我想考虑在它们旋转时压缩它们。

我快速浏览了扭曲的文档,但看不出如何使用 DailyLogFile 执行此操作类。

我的日志设置是:

from twisted.python.log import ILogObserver, FileLogObserver 
from twisted.python.logfile import DailyLogFile 
... 
"Setup the logging" 
logPath = os.getcwd() + "/logs/" 
logFile = DailyLogFile("lazarus.log", logPath, defaultMode=0644) 
application.setComponent(ILogObserver, FileLogObserver(logFile).emit) 

有人知道怎么做吗?

请您参考如下方法:

您可以继承DailyLogFile 并重载rotate 方法:

class DailyCompressedLogFile(DailyLogFile): 
    def rotate(self): 
        super(DailyCompressedLogFile, self).rotate() 
        newpath = "%s.%s" % (self.path, self.suffix(self.lastDate)) 
        if os.path.exists(newpath): 
            # compress newpath here