checkSize() function optimized
This commit is contained in:
parent
6f8d7c2759
commit
4ac4cb8e91
@ -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/".
|
* 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:
|
with open(logs_folder + 'latest.log', 'rb') as f_in:
|
||||||
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:
|
||||||
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()
|
except FileNotFoundError:
|
||||||
i = 2
|
log = open(logs_folder + 'latest.log', 'a')
|
||||||
except FileNotFoundError:
|
open(logs_folder + 'latest.log', 'a').close()
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def logWrite(message: str, logs_folder=f"logs{sep}", level="INFO") -> None:
|
def logWrite(message: str, logs_folder=f"logs{sep}", level="INFO") -> None:
|
||||||
|
Reference in New Issue
Block a user