🐛 修复了日志文件夹位置不正确的问题

This commit is contained in:
Nanaloveyuki
2025-07-29 17:33:38 +08:00
parent 80e4a4d02f
commit d1422513e9
3 changed files with 78 additions and 2 deletions

View File

@ -12,6 +12,8 @@ from ..utils.configs import get_config, set_config
from ..utils.time import get_asctime from ..utils.time import get_asctime
import pathlib import pathlib
from pathlib import Path from pathlib import Path
import sys
import os
def _get_full_path(file_path, file_name): def _get_full_path(file_path, file_name):
file_path.mkdir(parents=True, exist_ok=True) file_path.mkdir(parents=True, exist_ok=True)
@ -19,7 +21,18 @@ def _get_full_path(file_path, file_name):
class Logger: class Logger:
def __init__(self): 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 = project_root / get_config("file_path")
file_path.mkdir(parents=True, exist_ok=True) file_path.mkdir(parents=True, exist_ok=True)
if file_path.exists(): if file_path.exists():
@ -34,7 +47,18 @@ class Logger:
self.debug("日志文件已存在,已自动重命名") self.debug("日志文件已存在,已自动重命名")
def _log(self, msg, pf, lvn, no_file: bool = False, no_console: bool = False): 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 = project_root / get_config("file_path")
file_path.mkdir(parents=True, exist_ok=True) file_path.mkdir(parents=True, exist_ok=True)
file_name = get_config("file_name") file_name = get_config("file_name")

37
tests/t-multithread.py Normal file
View File

@ -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)

15
tests/t-repeat.py Normal file
View File

@ -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