"""
|
异步日志管理器
|
"""
|
import logging
|
import queue
|
import time
|
|
from log_module.log import logger_system
|
from utils import tool
|
|
|
class AsyncLogManager:
|
|
def __init__(self):
|
self.__log_queue = queue.Queue(maxsize=10240)
|
|
def __add_log(self, logger, method, *args):
|
try:
|
self.__log_queue.put_nowait((logger, time.time(), method, args))
|
except Exception:
|
pass
|
|
def add_log(self, data):
|
try:
|
self.__log_queue.put_nowait(data)
|
except Exception:
|
pass
|
|
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 get_queue_size(self):
|
"""
|
获取队列大小
|
@return:
|
"""
|
self.__log_queue.qsize()
|
|
# 运行同步日志
|
def run_sync(self, add_to_common_log=False):
|
# print("run_sync", add_to_common_log)
|
logger_system.info(f"run_sync 线程ID:{tool.get_thread_id()}")
|
while True:
|
try:
|
val = self.__log_queue.get()
|
if not add_to_common_log:
|
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)
|
else:
|
_common_log.add_log(val)
|
except Exception as e:
|
logging.exception(e)
|
|
|
l2_data_log = AsyncLogManager()
|
|
huaxin_l2_log = AsyncLogManager()
|
|
_common_log = AsyncLogManager()
|
|
|
def debug(logger, *args):
|
_common_log.debug(logger, *args)
|
|
|
def info(logger, *args):
|
_common_log.info(logger, *args)
|
|
|
def warning(logger, *args):
|
_common_log.warning(logger, *args)
|
|
|
def error(logger, *args):
|
_common_log.error(logger, *args)
|
|
|
def exception(logger, *args):
|
_common_log.exception(logger, *args)
|
|
|
# 运行同步日志
|
def run_sync():
|
logger_system.info(f"async_log 线程ID:{tool.get_thread_id()}")
|
_common_log.run_sync()
|
|
|
if __name__ == "__main__":
|
_queue = queue.Queue(maxsize=102400)
|
for i in range(200):
|
_queue.put_nowait("1")
|