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
logger.initialize({
enabled: CONFIG.LOG_ENABLED,
outputMode: CONFIG.LOG_OUTPUT_MODE,
logLevel: CONFIG.LOG_LEVEL,
logDir: CONFIG.LOG_DIR,
includeRequestId: CONFIG.LOG_INCLUDE_REQUEST_ID,
includeTimestamp: CONFIG.LOG_INCLUDE_TIMESTAMP,
maxFileSize: CONFIG.LOG_MAX_FILE_SIZE,
maxFiles: CONFIG.LOG_MAX_FILES
enabled: CONFIG.LOG_ENABLED ?? true,
outputMode: CONFIG.LOG_OUTPUT_MODE || "all",
logLevel: CONFIG.LOG_LEVEL || "info",
logDir: CONFIG.LOG_DIR || "logs",
includeRequestId: CONFIG.LOG_INCLUDE_REQUEST_ID ?? true,
includeTimestamp: CONFIG.LOG_INCLUDE_TIMESTAMP ?? true,
maxFileSize: CONFIG.LOG_MAX_FILE_SIZE || 10485760,
maxFiles: CONFIG.LOG_MAX_FILES || 10
});
// Cleanup old logs periodically

View file

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