diff --git a/src/logiliteal/levels/levels.py b/src/logiliteal/levels/levels.py index 4bd9942..a13d782 100644 --- a/src/logiliteal/levels/levels.py +++ b/src/logiliteal/levels/levels.py @@ -12,6 +12,8 @@ from ..utils.configs import get_config, set_config from ..utils.time import get_asctime import pathlib from pathlib import Path +import sys +import os def _get_full_path(file_path, file_name): file_path.mkdir(parents=True, exist_ok=True) @@ -19,7 +21,18 @@ def _get_full_path(file_path, file_name): class Logger: def __init__(self): - project_root = Path(__file__).parent.parent.parent.parent + # 检测虚拟环境并确定项目根目录 + python_path = sys.executable + project_root = Path.cwd() + + # 如果在虚拟环境中,向上查找项目根目录 + if 'venv' in python_path.lower(): + # 分割路径并查找venv目录 + path_parts = os.path.normpath(python_path).split(os.sep) + if 'venv' in path_parts: + venv_index = path_parts.index('venv') + project_root = Path(os.sep.join(path_parts[:venv_index])) + file_path = project_root / get_config("file_path") file_path.mkdir(parents=True, exist_ok=True) if file_path.exists(): @@ -34,7 +47,18 @@ class Logger: self.debug("日志文件已存在,已自动重命名") def _log(self, msg, pf, lvn, no_file: bool = False, no_console: bool = False): - project_root = Path(__file__).parent.parent.parent.parent + # 检测虚拟环境并确定项目根目录 + python_path = sys.executable + project_root = Path.cwd() + + # 如果在虚拟环境中,向上查找项目根目录 + if 'venv' in python_path.lower(): + # 分割路径并查找venv目录 + path_parts = os.path.normpath(python_path).split(os.sep) + if 'venv' in path_parts: + venv_index = path_parts.index('venv') + project_root = Path(os.sep.join(path_parts[:venv_index])) + file_path = project_root / get_config("file_path") file_path.mkdir(parents=True, exist_ok=True) file_name = get_config("file_name") diff --git a/tests/t-multithread.py b/tests/t-multithread.py new file mode 100644 index 0000000..04e043e --- /dev/null +++ b/tests/t-multithread.py @@ -0,0 +1,37 @@ +import sys +from pathlib import Path + +project_root = Path(__file__).parent.parent +sys.path.append(str(project_root)) + +from src.logiliteal import Logger + +log = Logger() + +import threading +import time +import random + +# 定义线程测试函数 +def thread_logger(thread_id: int): + """ + 线程日志测试函数 + Thread logger test function + Args: + thread_id (int): 线程ID Thread ID + """ + for i in range(10): + log.info(f"线程{thread_id}的第{i+1}条日志消息", no_file=True) + time.sleep(random.uniform(0.01, 0.05)) + +if __name__ == "__main__": + threads = [] + for i in range(10): + thread = threading.Thread(target=thread_logger, args=(i,)) + threads.append(thread) + thread.start() + + for thread in threads: + thread.join() + + log.info("所有线程日志记录完成", no_file=True) \ No newline at end of file diff --git a/tests/t-repeat.py b/tests/t-repeat.py new file mode 100644 index 0000000..1e559c0 --- /dev/null +++ b/tests/t-repeat.py @@ -0,0 +1,15 @@ +import sys +from pathlib import Path + +project_root = Path(__file__).parent.parent +sys.path.append(str(project_root)) + +from src.logiliteal import Logger + +log = Logger() + +i = 0 + +while i < 20000: + log.info(f"**md** | <#ff0000>测试--重--复~~日志~~: {i}") + i += 1 \ No newline at end of file