mirror of
https://github.com/kuitoi/kuitoi-Server.git
synced 2025-08-17 16:25:36 +00:00
Create log history
This commit is contained in:
parent
bd7b988b01
commit
fc886ef415
@ -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))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user