Administrator
2024-03-20 1be8fb3aed3f8a55c0dcb6d021685cbb2c79e6ec
log_module/async_log_util.py
@@ -50,55 +50,33 @@
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")
__common_log = AsyncLogManager()
def debug(logger, *args):
    __add_log(logger, True, "debug", *args)
    __common_log.debug(logger, *args)
def info(logger, *args):
    __add_log(logger, True, "info", *args)
    __common_log.info(logger, *args)
def warning(logger, *args):
    __add_log(logger, True, "warning", *args)
    __common_log.warning(logger, *args)
def error(logger, *args):
    __add_log(logger, True, "error", *args)
    __common_log.error(logger, *args)
def exception(logger, *args):
    __add_log(logger, True, "exception", *args)
    __common_log.exception(logger, *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][:6]}] " + d[0]
            d = tuple(d)
            method(*d)
        except:
            pass
    __common_log.run_sync()
if __name__ == "__main__":