fix: 为日志配置提供默认值并改进插件配置合并逻辑

- 为日志配置项添加默认值,避免未配置时出现 undefined
- 修改插件配置加载逻辑,始终生成默认配置并与本地配置合并
- 保留本地配置中的 enabled 字段,不因默认配置而覆盖
This commit is contained in:
hex2077 2026-01-25 20:31:56 +08:00
parent ef10cd8fb6
commit e41eb3491a
2 changed files with 32 additions and 13 deletions

View file

@ -232,14 +232,14 @@ export async function initializeConfig(args = process.argv.slice(2), configFileP
// Initialize logger // Initialize logger
logger.initialize({ logger.initialize({
enabled: CONFIG.LOG_ENABLED, enabled: CONFIG.LOG_ENABLED ?? true,
outputMode: CONFIG.LOG_OUTPUT_MODE, outputMode: CONFIG.LOG_OUTPUT_MODE || "all",
logLevel: CONFIG.LOG_LEVEL, logLevel: CONFIG.LOG_LEVEL || "info",
logDir: CONFIG.LOG_DIR, logDir: CONFIG.LOG_DIR || "logs",
includeRequestId: CONFIG.LOG_INCLUDE_REQUEST_ID, includeRequestId: CONFIG.LOG_INCLUDE_REQUEST_ID ?? true,
includeTimestamp: CONFIG.LOG_INCLUDE_TIMESTAMP, includeTimestamp: CONFIG.LOG_INCLUDE_TIMESTAMP ?? true,
maxFileSize: CONFIG.LOG_MAX_FILE_SIZE, maxFileSize: CONFIG.LOG_MAX_FILE_SIZE || 10485760,
maxFiles: CONFIG.LOG_MAX_FILES maxFiles: CONFIG.LOG_MAX_FILES || 10
}); });
// Cleanup old logs periodically // Cleanup old logs periodically

View file

@ -64,17 +64,36 @@ class PluginManager {
/** /**
* 加载插件配置文件 * 加载插件配置文件
* 永远生成默认配置如果本地文件存在则合并但不覆盖 enabled 字段
*/ */
async loadConfig() { async loadConfig() {
try { try {
// 1. 永远生成默认配置
const defaultConfig = await this.generateDefaultConfig();
// 2. 如果本地文件存在,读取并合并
if (existsSync(PLUGINS_CONFIG_FILE)) { if (existsSync(PLUGINS_CONFIG_FILE)) {
const content = await fs.readFile(PLUGINS_CONFIG_FILE, 'utf8'); const content = await fs.readFile(PLUGINS_CONFIG_FILE, 'utf8');
this.pluginsConfig = JSON.parse(content); const localConfig = JSON.parse(content);
} else {
// 扫描 plugins 目录生成默认配置 // 3. 合并配置:遍历默认配置中的所有插件
this.pluginsConfig = await this.generateDefaultConfig(); for (const [pluginName, defaultPluginConfig] of Object.entries(defaultConfig.plugins)) {
await this.saveConfig(); const localPluginConfig = localConfig.plugins?.[pluginName];
if (localPluginConfig) {
// 本地配置存在,合并但保留本地的 enabled 字段
defaultConfig.plugins[pluginName] = {
...defaultPluginConfig,
...localPluginConfig,
enabled: localPluginConfig.enabled // 保留本地的 enabled 字段
};
}
// 如果本地配置不存在该插件,使用默认配置
}
} }
this.pluginsConfig = defaultConfig;
await this.saveConfig();
} catch (error) { } catch (error) {
logger.error('[PluginManager] Failed to load config:', error.message); logger.error('[PluginManager] Failed to load config:', error.message);
this.pluginsConfig = { plugins: {} }; this.pluginsConfig = { plugins: {} };