mirror of
https://github.com/kuitoi/kuitoi-Server.git
synced 2026-06-18 14:41:21 +00:00
Create log history
This commit is contained in:
+18
-4
@@ -4,18 +4,32 @@
|
|||||||
# Version 1.0
|
# Version 1.0
|
||||||
# Licence: FPA
|
# Licence: FPA
|
||||||
# (c) kuitoi.su 2023
|
# (c) kuitoi.su 2023
|
||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import tarfile
|
||||||
|
|
||||||
log_format = "[%(asctime)s | %(name)-14s | %(levelname)-5s] %(message)s"
|
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_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
|
log_level = logging.INFO
|
||||||
# Инициализируем логирование
|
# Инициализируем логирование
|
||||||
logging.basicConfig(level=log_level, format=log_format)
|
logging.basicConfig(level=log_level, format=log_format)
|
||||||
# Настройка логирование в файл.
|
# Настройка логирование в файл.
|
||||||
# if os.path.exists(log_file):
|
if not os.path.exists(log_dir):
|
||||||
# os.remove(log_file)
|
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 = logging.FileHandler(log_file, encoding='utf-8')
|
||||||
fh.setFormatter(logging.Formatter(log_format))
|
fh.setFormatter(logging.Formatter(log_format))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user