feat: 历史日志

This commit is contained in:
bietiaop
2024-11-29 20:35:56 +08:00
committed by 手瓜一十雪
parent 7087eafe37
commit 9b04aed8b3
4 changed files with 54 additions and 1 deletions

19
src/webui/src/api/Log.ts Normal file
View File

@@ -0,0 +1,19 @@
import type { RequestHandler } from 'express';
import { sendError, sendSuccess } from '../utils/response';
import { WebUiConfigWrapper } from '../helper/config';
// 日志记录
export const LogHandler: RequestHandler = async (req, res) => {
const filename = req.query.id as string;
if (filename.includes('..')) {
return sendError(res, 'ID不合法');
}
const logContent = WebUiConfigWrapper.GetLogContent(filename);
return sendSuccess(res, logContent);
};
// 日志列表
export const LogListHandler: RequestHandler = async (_, res) => {
const logList = WebUiConfigWrapper.GetLogsList();
return sendSuccess(res, logList);
};

View File

@@ -1,5 +1,5 @@
import { webUiPathWrapper } from '@/webui';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { existsSync, readFileSync, writeFileSync, readdirSync } from 'node:fs';
import * as net from 'node:net';
import { resolve } from 'node:path';
@@ -131,4 +131,26 @@ export class WebUiConfigWrapper {
}
return defaultconfig; // 理论上这行代码到不了,到了只能返回默认配置了
}
// 获取日志文件夹路径
public static async GetLogsPath(): Promise<string> {
return resolve(webUiPathWrapper.logsPath);
}
// 获取日志列表
public static GetLogsList(): string[] {
if (existsSync(webUiPathWrapper.logsPath)) {
return readdirSync(webUiPathWrapper.logsPath)
.filter((file) => file.endsWith('.log'))
.map((file) => file.replace('.log', ''));
}
return [];
}
// 获取指定日志文件内容
public static GetLogContent(filename: string): string {
const logPath = resolve(webUiPathWrapper.logsPath, `${filename}.log`);
if (existsSync(logPath)) {
return readFileSync(logPath, 'utf-8');
}
return '';
}
}

View File

@@ -0,0 +1,9 @@
import { Router } from 'express';
import { LogHandler, LogListHandler } from '../api/Log';
const router = Router();
// router:读取日志内容
router.get('/GetLog', LogHandler);
// router:读取日志列表
router.get('/GetLogList', LogListHandler);
export { router as LogRouter };

View File

@@ -10,6 +10,7 @@ import { sendSuccess } from '@webapi/utils/response';
import { QQLoginRouter } from '@webapi/router/QQLogin';
import { AuthRouter } from '@webapi/router/auth';
import { LogRouter } from '@webapi/router/Log';
const router = Router();
@@ -26,5 +27,7 @@ router.use('/auth', AuthRouter);
router.use('/QQLogin', QQLoginRouter);
// router:OB11配置相关路由
router.use('/OB11Config', OB11ConfigRouter);
// router:日志相关路由
router.use('/Log', LogRouter);
export { router as ALLRouter };