From fb795c45d37da8da001b206c1830913cb21b5bfc Mon Sep 17 00:00:00 2001 From: TomyJan Date: Sat, 3 Feb 2024 22:36:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=AD=89=E7=BA=A7=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/config.js | 38 ++++++++++++++++++++++++++++++++++++++ components/logger.js | 8 +++++--- guoba.support.js | 4 +++- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 components/config.js diff --git a/components/config.js b/components/config.js new file mode 100644 index 0000000..6d42326 --- /dev/null +++ b/components/config.js @@ -0,0 +1,38 @@ +import fs from 'fs'; +import { _CfgPath } from '../data/system/pluginConstants.js'; +import kuroLogger from './logger.js'; + +class ConfigReader { + constructor() { + this.filePath = _CfgPath + '/config.json'; + this.configObject = this.readConfig(); // 初始读取配置文件 + this.watchConfig(); // 监听配置文件变化 + } + + readConfig() { + try { + const data = fs.readFileSync(this.filePath, 'utf8'); + const configObject = JSON.parse(data); + return configObject; + } catch (error) { + kuroLogger.error('读取配置文件失败:', error.message); + return {}; + } + } + + watchConfig() { + fs.watchFile(this.filePath, (curr, prev) => { + if (curr.mtime > prev.mtime) { + this.configObject = this.readConfig(); + kuroLogger.info('配置文件已更新'); + kuroLogger.setLogLevel(this.configObject.logger.logLevel); + } + }); + } + + getConfig() { + return this.configObject; + } +} + +export default new ConfigReader().readConfig(); diff --git a/components/logger.js b/components/logger.js index 52edd2a..a6e9ac7 100644 --- a/components/logger.js +++ b/components/logger.js @@ -1,11 +1,12 @@ import chalk from 'chalk' import fs from 'fs' import path from 'path' -import { dataPath, pluginVer } from '../data/system/pluginConstants.js' +import { dataPath } from '../data/system/pluginConstants.js' +import config from './config.js' class Logger { constructor( - logLevel = pluginVer.slice(-3) === 'rel' ? 'info' : 'debug', + logLevel = config.logger.logLevel || 'info', logDirectory = dataPath + '/logs' ) { this.logLevel = logLevel.toLowerCase() @@ -94,7 +95,8 @@ class Logger { } // 初始化全局日志记录器实例 -const kuroLogger = new Logger('debug') +console.log(JSON.stringify(config)) +const kuroLogger = new Logger(config.logger.logLevel || 'info') logger.info(chalk.gray(`[库洛插件][LOGGER] Logger initialized!`)) export default kuroLogger diff --git a/guoba.support.js b/guoba.support.js index b7ba928..f629bb7 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -91,8 +91,10 @@ export function supportGuoba() { // 比较配置文件更新 let testConfigJson = mergeObjects(defaultConfigJson, configJson) - if (testConfigJson !== configJson) { + if (JSON.stringify(testConfigJson) !== JSON.stringify(configJson)) { kuroLogger.warn('配置文件有更新, 建议检查是否有新的项目需要配置!') + kuroLogger.debug('testConfigJson:', JSON.stringify(testConfigJson)) + kuroLogger.debug('configJson:', JSON.stringify(configJson)) configJson = testConfigJson updateConfigFile() sendMsgFriend(