-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
49 lines (36 loc) · 1.69 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
import os
import zipfile
from logging import Formatter
from logging.handlers import TimedRotatingFileHandler
from app import app
class ZipTimedRotatingFileHandler(TimedRotatingFileHandler):
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False,
atTime=None):
super().__init__(filename, when, interval, backupCount, encoding, delay, utc, atTime)
def make_zip(self) -> None:
dir_path, base_filename = os.path.split(self.baseFilename)
logs_list = [f for f in os.listdir(dir_path)
if all([f.startswith(base_filename), f != base_filename, not f.endswith('.zip')])]
if len(logs_list) >= self.backupCount:
with zipfile.ZipFile('archive_{}.zip'.format(logs_list[0]), 'w') as zip_file:
for f in logs_list:
file = os.path.join(dir_path, f)
zip_file.write(file, os.path.base_filename(file), compress_type=zipfile.ZIP_DEFLATED)
os.remove(file)
def doRollover(self) -> None:
if self.backupCount > 0:
self.make_zip()
super().doRollover()
def getFilesToDelete(self) -> list:
return []
def _clear_existing_logger_handler() -> None:
logger = logging.getLogger(__name__)
if logger.hasHandlers():
logger.handlers.clear()
def get_logger_handler():
_clear_existing_logger_handler()
handler = ZipTimedRotatingFileHandler(app.config['LOGFILE'], when="midnight", interval=1, backupCount=3)
handler.setLevel(app.config['LOGGER_LEVEL'])
handler.setFormatter(Formatter('%(asctime)s | %(levelname)s | %(message)s '))
return handler