From c5c12db2e0dd772f40e9ee54a1d2b3a806f8ffc6 Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 15 Sep 2022 14:32:21 +0200 Subject: [PATCH] checkSize() function optimized --- modules/functions.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/modules/functions.py b/modules/functions.py index 243b96c..f9b3002 100644 --- a/modules/functions.py +++ b/modules/functions.py @@ -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: