checkSize() function optimized

This commit is contained in:
Profitroll 2022-09-15 14:31:49 +02:00
parent 6f8d7c2759
commit 4ac4cb8e91

View File

@ -30,28 +30,17 @@ def checkSize(logs_folder=f"logs{sep}", log_size=1024) -> None:
* logs_folder (str, optional): Folder where logs stored. Defaults to "logs/".
* log_size (int, optional): How many bytes should file containt to be rotated. Defaults to 1024.
"""
i = 0
while i < 2:
try:
log = stat(logs_folder + 'latest.log')
if (log.st_size / 1024) > log_size:
with open(logs_folder + 'latest.log', 'rb') as f_in:
with gzipopen(f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip', 'wb') as f_out:
copyfileobj(f_in, f_out)
open(logs_folder + 'latest.log', 'w').close()
i = 2
except FileNotFoundError:
try:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
try:
makedirs(logs_folder, exist_ok=True)
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
except:
pass
i += 1
makedirs(logs_folder, exist_ok=True)
try:
log = stat(logs_folder + 'latest.log')
if (log.st_size / 1024) > log_size:
with open(logs_folder + 'latest.log', 'rb') as f_in:
with gzipopen(f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip', 'wb') as f_out:
copyfileobj(f_in, f_out)
open(logs_folder + 'latest.log', 'w').close()
except FileNotFoundError:
log = open(logs_folder + 'latest.log', 'a')
open(logs_folder + 'latest.log', 'a').close()
def logWrite(message: str, logs_folder=f"logs{sep}", level="INFO") -> None: