refactor: log file limit

This commit is contained in:
手瓜一十雪
2024-06-19 23:26:05 +08:00
parent 5b42f8b743
commit b863896249
3 changed files with 29 additions and 6 deletions

View File

@@ -4,7 +4,7 @@ import fs from 'fs/promises';
import { log, logDebug } from './log'; import { log, logDebug } from './log';
import { dirname } from 'node:path'; import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import * as fsPromise from 'node:fs/promises';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); const __dirname = dirname(__filename);
@@ -262,3 +262,25 @@ export function isEqual(obj1: any, obj2: any) {
} }
return true; return true;
} }
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);
}
}

View File

@@ -39,7 +39,7 @@ const logConfig: Configuration = {
FileAppender: { // 输出到文件的appender FileAppender: { // 输出到文件的appender
type: 'file', type: 'file',
filename: logPath, // 指定日志文件的位置和文件名 filename: logPath, // 指定日志文件的位置和文件名
maxLoogSize: 10485760, // 日志文件的最大大小单位字节这里设置为10MB maxLogSize: 10485760, // 日志文件的最大大小单位字节这里设置为10MB
layout: { layout: {
type: 'pattern', type: 'pattern',
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %X{userInfo} | %m' pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %X{userInfo} | %m'

View File

@@ -9,7 +9,7 @@ import { log, logDebug, logError, LogLevel, logWarn, setLogLevel } from '@/commo
import { NapCatOnebot11 } from '@/onebot11/main'; import { NapCatOnebot11 } from '@/onebot11/main';
import { InitWebUi } from './webui/index'; import { InitWebUi } from './webui/index';
import { WebUiDataRuntime } from './webui/src/helper/Data'; 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 { dirname } from 'node:path';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import chalk from 'chalk'; import chalk from 'chalk';
@@ -22,6 +22,7 @@ program
.option('-q, --qq <type>', 'QQ号') .option('-q, --qq <type>', 'QQ号')
.parse(process.argv); .parse(process.argv);
//deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch();
// UpdateConfig().catch(logError); 移除支持 // UpdateConfig().catch(logError); 移除支持
// 启动WebUi // 启动WebUi
InitWebUi(); InitWebUi();
@@ -40,10 +41,10 @@ checkVersion().then(async (remoteVersion: string) => {
break; break;
} }
} }
logDebug(tagColor('[NapCat]'),'当前已是最新版本'); logDebug(tagColor('[NapCat]'), '当前已是最新版本');
return; return;
}).catch((e) => { }).catch((e) => {
logError(tagColor('[NapCat]'),'检测更新失败', e); logError(tagColor('[NapCat]'), '检测更新失败', e);
}); });
// 不是很好待优化 // 不是很好待优化
const NapCat_OneBot11 = new NapCatOnebot11(); const NapCat_OneBot11 = new NapCatOnebot11();