feat: logging
This commit is contained in:
35
logger/gunzip_rotating_file_handler.py
Normal file
35
logger/gunzip_rotating_file_handler.py
Normal file
@ -0,0 +1,35 @@
|
||||
import gzip
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import shutil
|
||||
import os
|
||||
|
||||
|
||||
class GunZipRotatingFileHandler(RotatingFileHandler):
|
||||
def doRollover(self):
|
||||
if self.stream:
|
||||
self.stream.close()
|
||||
self.stream = None
|
||||
|
||||
if self.backupCount > 0:
|
||||
# Rotate existing backup files
|
||||
for i in range(self.backupCount - 1, 0, -1):
|
||||
sfn = self.rotation_filename("%s.%d.gz" % (self.baseFilename, i))
|
||||
dfn = self.rotation_filename("%s.%d.gz" % (self.baseFilename, i + 1))
|
||||
if os.path.exists(sfn):
|
||||
if os.path.exists(dfn):
|
||||
os.remove(dfn)
|
||||
os.rename(sfn, dfn)
|
||||
|
||||
# Compress current log file to .1.gz
|
||||
dfn = self.rotation_filename(self.baseFilename + ".1.gz")
|
||||
if os.path.exists(dfn):
|
||||
os.remove(dfn)
|
||||
|
||||
if os.path.exists(self.baseFilename):
|
||||
with open(self.baseFilename, "rb") as f_in:
|
||||
with gzip.open(dfn, "wb") as f_out:
|
||||
shutil.copyfileobj(f_in, f_out)
|
||||
os.remove(self.baseFilename)
|
||||
|
||||
if not self.delay:
|
||||
self.stream = self._open()
|
||||
Reference in New Issue
Block a user