后端改为使用日志记录器管理日志

This commit is contained in:
2025-08-31 18:55:22 +08:00
parent ad5dfc64cb
commit a09d6cab0e
30 changed files with 962 additions and 323 deletions
+17 -12
View File
@@ -1,5 +1,10 @@
const fs = require('fs').promises;
const path = require('path');
const { defaultLogger } = require('../utils/logger');
// 创建logger实例
const logger = defaultLogger.child('CacheConfigManager');
/**
* 缓存配置管理器
@@ -58,10 +63,10 @@ class CacheConfigManager {
const configDir = path.dirname(this.configPath);
if (!require('fs').existsSync(configDir)) {
require('fs').mkdirSync(configDir, { recursive: true });
// console.log('缓存配置目录创建成功:', configDir);
// logger.info('缓存配置目录创建成功:', configDir);
}
} catch (error) {
console.error('创建缓存配置目录失败:', error);
logger.error('创建缓存配置目录失败:', error);
}
}
@@ -72,10 +77,10 @@ class CacheConfigManager {
try {
// 检查配置文件是否存在
await fs.access(this.configPath);
// console.log('缓存配置文件已存在');
// logger.info('缓存配置文件已存在');
} catch (error) {
// 配置文件不存在,创建默认配置
console.log('创建默认缓存配置文件...');
logger.info('创建默认缓存配置文件...');
await this.createDefaultConfig();
}
}
@@ -87,9 +92,9 @@ class CacheConfigManager {
try {
const configContent = JSON.stringify(this.defaultConfig, null, 2);
await fs.writeFile(this.configPath, configContent, 'utf8');
// console.log('默认缓存配置文件创建成功:', this.configPath);
// logger.info('默认缓存配置文件创建成功:', this.configPath);
} catch (error) {
console.error('创建默认缓存配置文件失败:', error);
logger.error('创建默认缓存配置文件失败:', error);
throw error;
}
}
@@ -105,7 +110,7 @@ class CacheConfigManager {
// 合并默认配置,确保所有字段都存在
return { ...this.defaultConfig, ...config };
} catch (error) {
console.error('加载缓存配置失败:', error);
logger.error('加载缓存配置失败:', error);
return this.defaultConfig;
}
}
@@ -120,9 +125,9 @@ class CacheConfigManager {
const configContent = JSON.stringify(config, null, 2);
await fs.writeFile(this.configPath, configContent, 'utf8');
console.log('缓存配置保存成功');
logger.info('缓存配置保存成功');
} catch (error) {
console.error('保存缓存配置失败:', error);
logger.error('保存缓存配置失败:', error);
throw error;
}
}
@@ -137,7 +142,7 @@ class CacheConfigManager {
await this.saveConfig(newConfig);
return newConfig;
} catch (error) {
console.error('更新缓存配置失败:', error);
logger.error('更新缓存配置失败:', error);
throw error;
}
}
@@ -148,10 +153,10 @@ class CacheConfigManager {
async resetToDefault() {
try {
await this.saveConfig(this.defaultConfig);
console.log('缓存配置已重置为默认值');
logger.info('缓存配置已重置为默认值');
return this.defaultConfig;
} catch (error) {
console.error('重置缓存配置失败:', error);
logger.error('重置缓存配置失败:', error);
throw error;
}
}
+23 -18
View File
@@ -1,5 +1,10 @@
const fs = require('fs').promises
const path = require('path')
const { defaultLogger } = require('../utils/logger');
// 创建logger实例
const logger = defaultLogger.child('ConfigManager');
/**
* 配置管理器
@@ -41,10 +46,10 @@ class ConfigManager {
const configDirPath = path.dirname(this.configDir)
if (!require('fs').existsSync(configDirPath)) {
require('fs').mkdirSync(configDirPath, { recursive: true })
console.log('配置目录创建成功:', configDirPath)
logger.info('配置目录创建成功:', configDirPath)
}
} catch (error) {
console.error('创建配置目录失败:', error)
logger.error('创建配置目录失败:', error)
}
}
@@ -56,10 +61,10 @@ class ConfigManager {
try {
// 检查配置文件是否存在
await fs.access(this.configDir)
console.log('用户配置文件已存在')
logger.info('用户配置文件已存在')
} catch (error) {
// 配置文件不存在,创建默认配置
console.log('创建默认用户配置文件...')
logger.info('创建默认用户配置文件...')
await this.createDefaultConfig()
}
}
@@ -76,9 +81,9 @@ class ConfigManager {
// 检查目录是否创建成功
try {
await fs.access(configDirPath)
console.log('配置目录确认存在:', configDirPath)
logger.info('配置目录确认存在:', configDirPath)
} catch (accessError) {
console.error('配置目录访问失败:', accessError)
logger.error('配置目录访问失败:', accessError)
throw new Error(`无法访问配置目录: ${configDirPath}`)
}
@@ -89,13 +94,13 @@ class ConfigManager {
// 验证文件是否写入成功
try {
await fs.access(this.configDir)
console.log('默认配置文件创建成功:', this.configDir)
logger.info('默认配置文件创建成功:', this.configDir)
} catch (verifyError) {
console.error('配置文件验证失败:', verifyError)
logger.error('配置文件验证失败:', verifyError)
throw new Error('配置文件创建后无法访问')
}
} catch (error) {
console.error('创建默认配置文件失败:', error)
logger.error('创建默认配置文件失败:', error)
throw error
}
}
@@ -108,7 +113,7 @@ class ConfigManager {
// 首先检查文件是否存在
const exists = await this.configExists()
if (!exists) {
console.log('配置文件不存在,创建默认配置...')
logger.info('配置文件不存在,创建默认配置...')
await this.createDefaultConfig()
}
@@ -118,14 +123,14 @@ class ConfigManager {
// 合并默认配置,确保所有必要的字段都存在
return { ...this.defaultConfig, ...config }
} catch (error) {
console.error('读取配置文件失败:', error)
console.log('使用默认配置...')
logger.error('读取配置文件失败:', error)
logger.info('使用默认配置...')
// 如果读取失败,尝试创建默认配置
try {
await this.createDefaultConfig()
return { ...this.defaultConfig }
} catch (createError) {
console.error('创建默认配置也失败:', createError)
logger.error('创建默认配置也失败:', createError)
// 最后返回内存中的默认配置
return { ...this.defaultConfig }
}
@@ -149,10 +154,10 @@ class ConfigManager {
'utf8'
)
console.log('配置文件保存成功')
logger.info('配置文件保存成功')
return true
} catch (error) {
console.error('保存配置文件失败:', error)
logger.error('保存配置文件失败:', error)
throw error
}
}
@@ -167,7 +172,7 @@ class ConfigManager {
await this.saveConfig(newConfig)
return newConfig
} catch (error) {
console.error('更新配置失败:', error)
logger.error('更新配置失败:', error)
throw error
}
}
@@ -178,10 +183,10 @@ class ConfigManager {
async resetToDefault() {
try {
await this.saveConfig(this.defaultConfig)
console.log('配置已重置为默认值')
logger.info('配置已重置为默认值')
return this.defaultConfig
} catch (error) {
console.error('重置配置失败:', error)
logger.error('重置配置失败:', error)
throw error
}
}