LOGGING['handlers']['file'] = {
'level': 'DEBUG',
'class': 'awx.main.utils.handlers.CompressedRotatingFileHandler',
'filename': os.path.join(LOG_PATH, 'debug.log'),
'maxBytes': 1024*100,
'backupCount': 5
}
I have written this piece of code for file logging in my django application, however each of the log files do not exceed the given maxBytes. When the first file exceeds the maxBytes it immediately creates 5 new log files with name .1 .2, etc and zips them(I am using custom handler which overrides the RotatingFileHandler’s doRollover class). I am also attaching CompressedRotatingFileHandler class code for reference.
class CompressedRotatingFileHandler(RotatingFileHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def doRollover(self):
"""
Do a rollover, as described in __init__().
"""
if self.stream:
self.stream.close()
self.stream = None
if self.backupCount > 0:
for i in range(self.backupCount - 1, 0, -1):
sfn = f"{self.baseFilename}.{i}"
dfn = f"{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)
os.rename(self.baseFilename, dfn)
# Gather log files
log_files = [
f"{self.baseFilename}.{i}"
for i in range(1, self.backupCount + 1)
if os.path.exists(f"{self.baseFilename}.{i}")
]
# Get count of existing backup zip files
nzip_backups = len(glob.glob(f"{self.baseFilename}.backup.*.zip"))
# Check if the number of backup log files has reached the limit
if len(log_files) == self.backupCount:
zip_filename = f"{self.baseFilename}.backup.{nzip_backups + 1}.zip"
with zipfile.ZipFile(zip_filename, "w") as myzip:
for fn in log_files:
if os.path.exists(fn):
myzip.write(fn, os.path.basename(fn))
os.remove(fn)
time.sleep(1) # Optional delay to ensure the file is removed
self.mode = 'w'
self.stream = self._open()