feat: logging

This commit is contained in:
2025-07-28 15:27:17 +04:00
parent 361f94323c
commit 27fb24a44c
10 changed files with 171 additions and 5 deletions

View 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()