| | |
| | | class AsyncLogManager: |
| | | |
| | | def __init__(self): |
| | | self.__log_queue = queue.Queue() |
| | | self.__log_queue = queue.Queue(maxsize=10240) |
| | | |
| | | def __add_log(self, logger, method, *args): |
| | | self.__log_queue.put_nowait((logger, time.time(), method, args)) |
| | | try: |
| | | self.__log_queue.put_nowait((logger, time.time(), method, args)) |
| | | except Exception: |
| | | pass |
| | | |
| | | def add_log(self, data): |
| | | self.__log_queue.put_nowait(data) |
| | | try: |
| | | self.__log_queue.put_nowait(data) |
| | | except Exception: |
| | | pass |
| | | |
| | | def debug(self, logger, *args): |
| | | self.__add_log(logger, "debug", *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): |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | # info(logger_debug, "*-{}", "test") |
| | | asyncLogManager = AsyncLogManager() |
| | | asyncLogManager.info(logger_debug, "测试123") |
| | | threading.Thread(target=lambda: asyncLogManager.run_sync(), daemon=True).start() |
| | | time.sleep(1) |
| | | # info(logger_debug, "002375") |
| | | run_sync() |
| | | _queue = queue.Queue(maxsize=102400) |
| | | for i in range(200): |
| | | _queue.put_nowait("1") |
| | | |