日志优化

This commit is contained in:
Chen committed 2025-12-07 17:12:29 +08:00
1 parent fa5bb66d14
commit aeb403f3c0
21 files changed
+402 -209

No files matched your search

+15 -13
View File
@@ -3,6 +3,8 @@
* Executes routine and emergency memory cleanup procedures
*/
const logger = require('../src/utils/logger');
class CleanupManager {
constructor(options = {}) {
this.routineInterval = options.routineInterval || 5000; // 5 seconds
@@ -21,7 +23,7 @@ class CleanupManager {
startRoutineCleanup() {
if (this.isRunning) return;
console.log('🧹 清理管理器已启动');
logger.task('清理管理器已启动');
this.isRunning = true;
this.routineIntervalId = setInterval(() => {
@@ -35,7 +37,7 @@ class CleanupManager {
stopRoutineCleanup() {
if (!this.isRunning) return;
console.log('🧹 清理管理器已停止');
logger.task('清理管理器已停止');
if (this.routineIntervalId) {
clearInterval(this.routineIntervalId);
@@ -76,7 +78,7 @@ class CleanupManager {
// 只在内存回收显著时输出日志
if (Math.abs(rssReclaimed) > 10) {
console.log(`🧹 内存清理: ${rssBefore.toFixed(0)}MB → ${rssAfter.toFixed(0)}MB`);
logger.perf(`内存清理: ${rssBefore.toFixed(0)}MB → ${rssAfter.toFixed(0)}MB`);
}
return {
@@ -87,7 +89,7 @@ class CleanupManager {
connectionsCleared: 0
};
} catch (error) {
console.error('❌ 清理失败:', error.message);
logger.error('清理失败:', error.message);
return null;
}
}
@@ -99,7 +101,7 @@ class CleanupManager {
* - Force garbage collection multiple times (3+)
*/
executeEmergencyCleanup() {
console.log('🚨 执行紧急内存清理...');
logger.task('执行紧急内存清理...');
const memoryBefore = this.getMemoryStats();
@@ -110,15 +112,15 @@ class CleanupManager {
try {
this.dbPool.closeAll();
connectionsCleared = 1;
console.log(' ✓ 数据库连接已关闭');
logger.task('数据库连接已关闭');
} catch (error) {
console.error(' ✗ 关闭连接失败:', error.message);
logger.error('关闭连接失败:', error.message);
}
}
// 2. Clear all registered caches
const cachesCleared = this.clearAllCaches();
console.log(` ✓ 已清理 ${cachesCleared} 个缓存`);
logger.task(`已清理 ${cachesCleared} 个缓存`);
// 3. Force garbage collection multiple times (minimum 3)
this.forceGarbageCollection(3);
@@ -126,7 +128,7 @@ class CleanupManager {
const memoryAfter = this.getMemoryStats();
const memoryReclaimed = memoryBefore.heapUsed - memoryAfter.heapUsed;
console.log(`✅ 紧急清理完成: 回收 ${(memoryReclaimed / 1024 / 1024).toFixed(0)}MB`);
logger.task(`紧急清理完成: 回收 ${(memoryReclaimed / 1024 / 1024).toFixed(0)}MB`);
return {
memoryBefore,
@@ -136,7 +138,7 @@ class CleanupManager {
connectionsCleared
};
} catch (error) {
console.error('❌ 紧急清理失败:', error.message);
logger.error('紧急清理失败:', error.message);
return null;
}
}
@@ -153,7 +155,7 @@ class CleanupManager {
try {
global.gc();
} catch (error) {
console.error(` ✗ GC失败 (${i + 1}/${actualIterations}):`, error.message);
logger.error(`GC失败 (${i + 1}/${actualIterations}):`, error.message);
}
}
}
@@ -172,7 +174,7 @@ class CleanupManager {
cleared++;
}
} catch (error) {
console.error(`❌ 清理缓存失败 "${name}":`, error.message);
logger.error(`清理缓存失败 "${name}":`, error.message);
}
}
@@ -186,7 +188,7 @@ class CleanupManager {
*/
registerCache(name, cache) {
if (!cache || typeof cache.clear !== 'function') {
console.warn(`⚠️ 缓存 "${name}" 无clear()方法`);
logger.warn(`缓存 "${name}" 无clear()方法`);
return;
}