From fc886ef415e75935ffb65e437491c2811a7f7aaf Mon Sep 17 00:00:00 2001 From: SantaSpeen Date: Wed, 12 Jul 2023 23:58:14 +0300 Subject: [PATCH] Create log history --- src/core/utils.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/core/utils.py b/src/core/utils.py index dec2880..778619a 100644 --- a/src/core/utils.py +++ b/src/core/utils.py @@ -4,18 +4,32 @@ # Version 1.0 # Licence: FPA # (c) kuitoi.su 2023 - +import datetime import logging +import os +import tarfile log_format = "[%(asctime)s | %(name)-14s | %(levelname)-5s] %(message)s" log_format_access = '[%(asctime)s | %(name)-14s | %(levelname)-5s] %(client_addr)s - "%(request_line)s" %(status_code)s' -log_file = "server.log" +log_dir = "./logs/" +log_file = log_dir + "server.log" log_level = logging.INFO # Инициализируем логирование logging.basicConfig(level=log_level, format=log_format) # Настройка логирование в файл. -# if os.path.exists(log_file): -# os.remove(log_file) +if not os.path.exists(log_dir): + os.mkdir(log_dir) +if os.path.exists(log_file): + mtime = os.path.getmtime(log_file) + gz_path = log_dir + datetime.datetime.fromtimestamp(mtime).strftime('%d.%m.%Y') + "-%s.tar.gz" + index = 1 + while True: + if not os.path.exists(gz_path % index): + break + index += 1 + with tarfile.open(gz_path % index, "w:gz") as tar: + tar.add(log_file, os.path.basename(log_file)) + os.remove(log_file) fh = logging.FileHandler(log_file, encoding='utf-8') fh.setFormatter(logging.Formatter(log_format))