From b863896249857a2c10144f84ca39f8a30a0c91e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 19 Jun 2024 23:26:05 +0800 Subject: [PATCH] refactor: log file limit --- src/common/utils/helper.ts | 26 ++++++++++++++++++++++++-- src/common/utils/log.ts | 2 +- src/index.ts | 7 ++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index a2bfb0a1..ff1fe789 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -4,7 +4,7 @@ import fs from 'fs/promises'; import { log, logDebug } from './log'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; - +import * as fsPromise from 'node:fs/promises'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -261,4 +261,26 @@ export function isEqual(obj1: any, obj2: any) { if (!isEqual(obj1[key], obj2[key])) return false; } return true; -} \ No newline at end of file +} + +export async function deleteOldFiles(directoryPath: string, daysThreshold: number) { + try { + const files = await fsPromise.readdir(directoryPath); + + for (const file of files) { + const filePath = path.join(directoryPath, file); + const stats = await fsPromise.stat(filePath); + const lastModifiedTime = stats.mtimeMs; + const currentTime = Date.now(); + const timeDifference = currentTime - lastModifiedTime; + const daysDifference = timeDifference / (1000 * 60 * 60 * 24); + + if (daysDifference > daysThreshold) { + await fsPromise.unlink(filePath); // Delete the file + //console.log(`Deleted: ${filePath}`); + } + } + } catch (error) { + //console.error('Error deleting files:', error); + } +} diff --git a/src/common/utils/log.ts b/src/common/utils/log.ts index fb43c68c..cd6edcc8 100644 --- a/src/common/utils/log.ts +++ b/src/common/utils/log.ts @@ -39,7 +39,7 @@ const logConfig: Configuration = { FileAppender: { // 输出到文件的appender type: 'file', filename: logPath, // 指定日志文件的位置和文件名 - maxLoogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB + maxLogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %X{userInfo} | %m' diff --git a/src/index.ts b/src/index.ts index a8d5b2a6..de6bc80f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { log, logDebug, logError, LogLevel, logWarn, setLogLevel } from '@/commo import { NapCatOnebot11 } from '@/onebot11/main'; import { InitWebUi } from './webui/index'; import { WebUiDataRuntime } from './webui/src/helper/Data'; -import { UpdateConfig } from './common/utils/helper'; +import { deleteOldFiles, UpdateConfig } from './common/utils/helper'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; @@ -22,6 +22,7 @@ program .option('-q, --qq ', 'QQ号') .parse(process.argv); +//deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch(); // UpdateConfig().catch(logError); 移除支持 // 启动WebUi InitWebUi(); @@ -40,10 +41,10 @@ checkVersion().then(async (remoteVersion: string) => { break; } } - logDebug(tagColor('[NapCat]'),'当前已是最新版本'); + logDebug(tagColor('[NapCat]'), '当前已是最新版本'); return; }).catch((e) => { - logError(tagColor('[NapCat]'),'检测更新失败', e); + logError(tagColor('[NapCat]'), '检测更新失败', e); }); // 不是很好待优化 const NapCat_OneBot11 = new NapCatOnebot11();