Skip to content

Let's use AI to Earn!

feat: 日志清理

牛文政5c1d7fc89d1e
diff --git a/electron/global/log.ts b/electron/global/log.ts
index 6e9b2083..6ac3210d 100644
--- a/electron/global/log.ts
+++ b/electron/global/log.ts
@@ -50,3 +50,23 @@ export function getLogFilePaths(days: number): string[] {
return logFilePaths;
}
+
+/**
+ * 清理一周前的日志
+ */
+export function clearOldLogs() {
+ const logDirectory = path.join(app.getPath('appData'), 'logs');
+ const oneWeekAgo = Date.now() - 7 * 24 * 60 * 60 * 1000;
+ const logFiles = fs.readdirSync(logDirectory);
+ const logFilePaths = logFiles
+ .filter((file) => file.endsWith('.log'))
+ .map((file) => path.join(logDirectory, file))
+ .filter((filePath) => {
+ const fileStat = fs.statSync(filePath);
+ return fileStat.mtime.getTime() < oneWeekAgo;
+ });
+
+ logFilePaths.forEach((filePath) => {
+ fs.unlinkSync(filePath);
+ });
+}
diff --git a/electron/main/tools/controller.ts b/electron/main/tools/controller.ts
index c1447046..f0b27137 100644
--- a/electron/main/tools/controller.ts
+++ b/electron/main/tools/controller.ts
@@ -7,8 +7,9 @@
*/
import { FileUtils } from '../../util/file';
import { FFmpegVideoUtil } from '../../util/ffmpeg/video';
-import { Controller, Icp, Inject } from '../core/decorators';
+import { Controller, Icp, Inject, Scheduled } from '../core/decorators';
import { ToolsService } from './service';
+import { clearOldLogs } from '../../global/log';
@Controller()
export class ToolsController {
@@ -44,4 +45,11 @@ export class ToolsController {
return '';
}
}
+
+ // 定时清理日志, 每小时进行
+ @Scheduled('0 0 * * * *', 'clearLog')
+ async clearLog() {
+ console.log('clear Log ing ...');
+ clearOldLogs();
+ }
}