mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
系统日志增加时间筛选和清空
This commit is contained in:
+28
-2
@@ -15,6 +15,7 @@ import {
|
||||
parseVersion,
|
||||
promiseExec,
|
||||
readDirs,
|
||||
rmPath,
|
||||
} from '../config/util';
|
||||
import {
|
||||
DependenceModel,
|
||||
@@ -34,6 +35,7 @@ import NotificationService from './notify';
|
||||
import ScheduleService, { TaskCallbacks } from './schedule';
|
||||
import SockService from './sock';
|
||||
import os from 'os';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
@Service()
|
||||
export default class SystemService {
|
||||
@@ -409,9 +411,25 @@ export default class SystemService {
|
||||
}
|
||||
}
|
||||
|
||||
public async getSystemLog(res: Response) {
|
||||
public async getSystemLog(
|
||||
res: Response,
|
||||
query: {
|
||||
startTime?: string;
|
||||
endTime?: string;
|
||||
},
|
||||
) {
|
||||
const startTime = dayjs(query.startTime || undefined)
|
||||
.startOf('d')
|
||||
.valueOf();
|
||||
const endTime = dayjs(query.endTime || undefined)
|
||||
.endOf('d')
|
||||
.valueOf();
|
||||
const result = await readDirs(config.systemLogPath, config.systemLogPath);
|
||||
const logs = result.reverse().filter((x) => x.title.endsWith('.log'));
|
||||
const logs = result
|
||||
.reverse()
|
||||
.filter((x) => x.title.endsWith('.log'))
|
||||
.filter((x) => x.mtime >= startTime && x.mtime <= endTime);
|
||||
|
||||
res.set({
|
||||
'Content-Length': sum(logs.map((x) => x.size)),
|
||||
});
|
||||
@@ -433,4 +451,12 @@ export default class SystemService {
|
||||
}
|
||||
})(res, logs);
|
||||
}
|
||||
|
||||
public async deleteSystemLog() {
|
||||
const result = await readDirs(config.systemLogPath, config.systemLogPath);
|
||||
const logs = result.reverse().filter((x) => x.title.endsWith('.log'));
|
||||
for (const log of logs) {
|
||||
await rmPath(path.join(config.systemLogPath, log.title));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +154,7 @@ export default class UserService {
|
||||
status: LoginStatus.success,
|
||||
},
|
||||
});
|
||||
this.getLoginLog();
|
||||
return {
|
||||
code: 200,
|
||||
data: { token, lastip, lastaddr, lastlogon, retries, platform },
|
||||
@@ -182,6 +183,7 @@ export default class UserService {
|
||||
status: LoginStatus.fail,
|
||||
},
|
||||
});
|
||||
this.getLoginLog();
|
||||
if (retries > 2) {
|
||||
const waitTime = Math.round(Math.pow(3, retries + 1));
|
||||
return {
|
||||
@@ -215,8 +217,9 @@ export default class UserService {
|
||||
(a, b) => b.info!.timestamp! - a.info!.timestamp!,
|
||||
);
|
||||
if (result.length > 100) {
|
||||
const ids = result.slice(0, result.length - 100).map((x) => x.id!);
|
||||
await SystemModel.destroy({
|
||||
where: { id: result[result.length - 1].id },
|
||||
where: { id: ids },
|
||||
});
|
||||
}
|
||||
return result.map((x) => x.info);
|
||||
|
||||
Reference in New Issue
Block a user