Administrator
2024-03-05 39a820f66d4fe3fa6f3542882adf90fa1e076f1b
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
"""
异步日志管理器
"""
import queue
import time
 
from log_module.log import logger_debug, logger_system
from utils import tool
 
 
class AsyncLogManager:
    __log_queue = queue.Queue()
 
    def __add_log(self, logger, method, *args):
        self.__log_queue.put_nowait((logger, time.time(), method, args))
 
    def debug(self, logger, *args):
        self.__add_log(logger, "debug", *args)
 
    def info(self, logger, *args):
        self.__add_log(logger, "info", *args)
 
    def warning(self, logger, *args):
        self.__add_log(logger, "warning", *args)
 
    def error(self, logger, *args):
        self.__add_log(logger, "error", *args)
 
    def exception(self, logger, *args):
        self.__add_log(logger, "exception", *args)
 
    # 运行同步日志
    def run_sync(self):
        logger_system.info(f"run_sync 线程ID:{tool.get_thread_id()}")
        while True:
            try:
                val = self.__log_queue.get()
                time_s = val[1]
                cmd = val[2]
                method = getattr(val[0], cmd)
                d = list(val[3])
                d[0] = f"[{tool.to_time_str(int(time_s))}.{str(time_s).split('.')[1][:6]}] " + d[0]
                d = tuple(d)
                method(*d)
            except:
                pass
 
 
l2_data_log = AsyncLogManager()
 
huaxin_l2_log = AsyncLogManager()
 
log_queue = queue.Queue()
 
 
def __add_log(logger, time_out_log, method, *args):
    start_time = time.time()
    log_queue.put_nowait((logger, start_time, method, args))
    if time_out_log:
        end_time = time.time()
        sub_time = end_time - start_time
        if sub_time > 0.01:
            # 记录日志保存慢的日志
            __add_log(logger_debug, False, f"保存到日志队列用时:{sub_time}s")
 
 
def debug(logger, *args):
    __add_log(logger, True, "debug", *args)
 
 
def info(logger, *args):
    __add_log(logger, True, "info", *args)
 
 
def warning(logger, *args):
    __add_log(logger, True, "warning", *args)
 
 
def error(logger, *args):
    __add_log(logger, True, "error", *args)
 
 
def exception(logger, *args):
    __add_log(logger, True, "exception", *args)
 
 
# 运行同步日志
def run_sync():
    logger_system.info(f"async_log 线程ID:{tool.get_thread_id()}")
    while True:
        try:
            val = log_queue.get()
            time_s = val[1]
            cmd = val[2]
            method = getattr(val[0], cmd)
            d = list(val[3])
            d[0] = f"[{tool.to_time_str(int(time_s))}.{str(time_s).split('.')[1][:3]}] " + d[0]
            d = tuple(d)
            method(*d)
        except:
            pass
 
 
if __name__ == "__main__":
    # info(logger_debug, "*-{}", "test")
    info(logger_debug, "002375")
    run_sync()