checkSize() function optimized

This commit is contained in:
Profitroll 2022-09-15 14:32:21 +02:00
parent 585994df47
commit c5c12db2e0

View File

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