Full and configurable logfile rotation.

This commit is contained in:
K8 2026-05-28 13:11:00 -06:00
parent 956d688e10
commit 1694a17a75
No known key found for this signature in database

View file

@ -77,7 +77,8 @@ LOG_STDOUT = 0x91
LOG_FILE = 0x92
LOG_CALLBACK = 0x93
LOG_MAXSIZE = 5*1024*1024
LOG_MAXSIZE = 30*1024*1024
LOG_MAXROT = 9
loglevel = LOG_NOTICE
logfile = None
@ -144,9 +145,17 @@ def log(msg, level=3, _override_destination = False, pt=False):
try:
with open(logfile, "a") as file: file.write(logstring+"\n")
if os.path.getsize(logfile) > LOG_MAXSIZE:
prevfile = logfile+".1"
if os.path.isfile(prevfile): os.unlink(prevfile)
os.rename(logfile, prevfile)
for i in range(LOG_MAXROT, 0, -1):
oldfile = f"{logfile}.{i}"
if os.path.isfile(oldfile):
if i == LOG_MAXROT:
os.unlink(oldfile)
else:
rotfile = f"{logfile}.{i+1}"
os.rename(oldfile, rotfile)
rotfile = f"{logfile}.1"
os.rename(logfile, rotfile)
except Exception as e:
_always_override_destination = True