我正在使用Python中的标准日志记录库。例如,有RotatingFileHandler可以每天轮换日志文件。
但它只是重命名它们。如果它不仅可以重命名,还可以将旧文件放入zip(或gz,bzip等)归档文件中,那将是很好的。
有没有简单的方法来实现这一点?
我认为您最好的选择是扩展RotatingFileHandler,可以像下面这样做(未经测试):
import os
from logging.handlers import RotatingFileHandler
COMPRESSION_SUPPORTED = {}
try:
import gzip
COMPRESSION_SUPPORTED['gz'] = gzip
except ImportError:
pass
try:
import zipfile
COMPRESSION_SUPPORTED['zip'] = zipfile
except ImportError:
pass
class NewRotatingFileHandler(RotatingFileHandler):
def __init__(self, *args, **kws):
compress_mode = kws.pop('compress_mode')
try:
self.compress_cls = COMPRESSION_SUPPORTED[compress_mode]
except KeyError:
raise ValueError('"%s" compression method not supported.' % compress_mode)
super(NewRotatingFileHandler, self).__init__(self, *args, **kws)
def doRollover(self):
super(NewRotatingFileHandler, self).doRollover()
# Compress the old log.
old_log = self.baseFilename + ".1"
with open(old_log) as log:
with self.compress_cls.open(old_log + '.gz', 'wb') as comp_log:
comp_log.writelines(log)
os.remove(old_log)
backupCount > 1
的情况,因为压缩文件的重命名不正确。 - Jörn HeesbackupCount > 1
的情况:https://dev59.com/GFrUa4cB1Zd3GeqPgx40#35547094 - Thomasleveil采纳的答案只会归档一个文件-(basefile.log.1)。其他文件不会被归档。 这段代码将归档除基本文件之外的所有日志文件。
import os
import gzip
import logging.handlers
class NewRotatingFileHandler(logging.handlers.RotatingFileHandler):
def __init__(self, filename, **kws):
backupCount = kws.get('backupCount', 0)
self.backup_count = backupCount
logging.handlers.RotatingFileHandler.__init__(self, filename, **kws)
def doArchive(self, old_log):
with open(old_log) as log:
with gzip.open(old_log + '.gz', 'wb') as comp_log:
comp_log.writelines(log)
os.remove(old_log)
def doRollover(self):
if self.stream:
self.stream.close()
self.stream = None
if self.backup_count > 0:
for i in range(self.backup_count - 1, 0, -1):
sfn = "%s.%d.gz" % (self.baseFilename, i)
dfn = "%s.%d.gz" % (self.baseFilename, i + 1)
if os.path.exists(sfn):
if os.path.exists(dfn):
os.remove(dfn)
os.rename(sfn, dfn)
dfn = self.baseFilename + ".1"
if os.path.exists(dfn):
os.remove(dfn)
if os.path.exists(self.baseFilename):
os.rename(self.baseFilename, dfn)
self.doArchive(dfn)
if not self.delay:
self.stream = self._open()
encoding='bz2-codec'
来初始化RotatingFileHandler
,自动编写压缩日志文件:import logging
import logging.handlers as handlers
if __name__=='__main__':
log_filename='log_rotate.bz2'
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = handlers.RotatingFileHandler(
log_filename, maxBytes=20, backupCount=5, encoding='bz2-codec')
logger.addHandler(handler)
for i in range(20):
logger.debug('i = %d' % i)
PS. Python3已将'bz2-codec'从有效编码集中移除,因此本解决方案仅适用于Python2。