-
Notifications
You must be signed in to change notification settings - Fork 482
/
log.py
114 lines (94 loc) · 3.86 KB
/
log.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import logging
import os
import re
import threading
import time
from collections import deque
from html import escape
from logging.handlers import RotatingFileHandler
from config import Config
logging.getLogger('werkzeug').setLevel(logging.ERROR)
lock = threading.Lock()
LOG_QUEUE = deque(maxlen=200)
LOG_INDEX = 0
class Logger:
logger = None
__instance = {}
__config = None
__loglevels = {
"info": logging.INFO,
"debug": logging.DEBUG,
"error": logging.ERROR
}
def __init__(self, module):
self.logger = logging.getLogger(module)
self.__config = Config()
logtype = self.__config.get_config('app').get('logtype') or "console"
loglevel = self.__config.get_config('app').get('loglevel') or "info"
self.logger.setLevel(level=self.__loglevels.get(loglevel))
if logtype == "server":
logserver = self.__config.get_config('app').get('logserver', '').split(':')
if logserver:
logip = logserver[0]
if len(logserver) > 1:
logport = int(logserver[1] or '514')
else:
logport = 514
log_server_handler = logging.handlers.SysLogHandler((logip, logport),
logging.handlers.SysLogHandler.LOG_USER)
log_server_handler.setFormatter(logging.Formatter('%(filename)s: %(message)s'))
self.logger.addHandler(log_server_handler)
elif logtype == "file":
# 记录日志到文件
logpath = os.environ.get('NASTOOL_LOG') or self.__config.get_config('app').get('logpath') or ""
if logpath:
if not os.path.exists(logpath):
os.makedirs(logpath, exist_ok=True)
log_file_handler = RotatingFileHandler(filename=os.path.join(logpath, module + ".txt"),
maxBytes=5 * 1024 * 1024,
backupCount=3,
encoding='utf-8')
log_file_handler.setFormatter(logging.Formatter('%(asctime)s\t%(levelname)s: %(message)s'))
self.logger.addHandler(log_file_handler)
# 记录日志到终端
log_console_handler = logging.StreamHandler()
log_console_handler.setFormatter(logging.Formatter('%(asctime)s\t%(levelname)s: %(message)s'))
self.logger.addHandler(log_console_handler)
@staticmethod
def get_instance(module):
if not module:
module = "run"
if Logger.__instance.get(module):
return Logger.__instance.get(module)
with lock:
Logger.__instance[module] = Logger(module)
return Logger.__instance.get(module)
def __append_log_queue(level, text):
global LOG_INDEX, LOG_QUEUE
with lock:
text = escape(text)
if text.startswith("【"):
source = re.findall(r"(?<=【).*?(?=】)", text)[0]
text = text.replace(f"【{source}】", "")
else:
source = "System"
LOG_QUEUE.append({
"time": time.strftime('%H:%M:%S', time.localtime(time.time())),
"level": level,
"source": source,
"text": text})
LOG_INDEX += 1
def debug(text, module=None):
return Logger.get_instance(module).logger.debug(text)
def info(text, module=None):
__append_log_queue("INFO", text)
return Logger.get_instance(module).logger.info(text)
def error(text, module=None):
__append_log_queue("ERROR", text)
return Logger.get_instance(module).logger.error(text)
def warn(text, module=None):
__append_log_queue("WARN", text)
return Logger.get_instance(module).logger.warning(text)
def console(text):
__append_log_queue("INFO", text)
print(text)