import os # 获取项目根目录的绝对路径(MassageRobot_Dobot 目录) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def read_log_file(log_type, keyword=None, page=1 , page_size=400): """ 根据日志类型和关键字读取日志内容 :param log_type: 日志类型(ui/language/massage) :param keyword: 可选,筛选包含关键字的日志行 :param page: 页码 :param page_size: 每页显示的行数 :return: 匹配的日志内容(列表形式) """ # 日志文件路径映射 log_files = { "ui": os.path.join(BASE_DIR, "log", "UI-next-app.log"), # 使用绝对路径 "language": os.path.join(BASE_DIR, "log", "Language.log"), "massage": os.path.join(BASE_DIR, "log", "Massage.log") } print(os.path.join(BASE_DIR)) log_path = log_files.get(log_type) if not log_path or not os.path.exists(log_path): return {"error": "日志文件不存在"} # 读取日志文件内容 with open(log_path, "r", encoding="utf-8") as f: lines = f.readlines() # 按关键字筛选 if keyword: filtered_lines = [line.strip() for line in lines if keyword in line] else: filtered_lines = [line.strip() for line in lines] # 分页 start = (page - 1) * page_size end = start + page_size paginated_lines = filtered_lines[start:end] return { "data": paginated_lines, "total": len(lines), "page": page, "pageSize": page_size }