From f9b2f358287bfc7b3627eda3f4357df1a87b789e Mon Sep 17 00:00:00 2001 From: Silence_Lurker Date: Fri, 16 Jan 2026 10:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0Logger?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SL/bus/Amily2Bus.js | 18 ++++++++++++-- SL/bus/chain/Chain.js | 56 +++++++++++++++++++++++++++++++++++++++++++ SL/bus/log/Logger.js | 40 ++++++++++++++++++++----------- 3 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 SL/bus/chain/Chain.js diff --git a/SL/bus/Amily2Bus.js b/SL/bus/Amily2Bus.js index c758d3b..060db39 100644 --- a/SL/bus/Amily2Bus.js +++ b/SL/bus/Amily2Bus.js @@ -1,5 +1,5 @@ -import { Logger } from './core/logger.js'; -import { FilePipe } from './core/file-pipe.js'; +import Logger from './log/Logger.js'; +import FilePipe from './file/FilePipe.js'; class Amily2Bus { constructor() { @@ -14,6 +14,20 @@ class Amily2Bus { console.log('[Amily2Bus] Core container initialized with secure registry.'); } + /** + * 直接记录系统级日志 (Global Scope) + * 支持手动指定来源,方便终端调试或非插件环境调用 + * @param {string} type 日志级别 (debug, info, warn, error) + * @param {string} message 消息内容 + * @param {string} [origin='Bus'] 来源模块,默认为 'Bus' + * @param {string} [plugin='Global'] 来源插件/命名空间,调试时可指定如 'Console' + */ + log(type, message, origin = 'Bus', plugin = 'Global') { + if (this.Logger) { + this.Logger.process(plugin, origin, type, message); + } + } + /** * 注册插件并获取专属上下文 * @param {string} pluginName 插件名称 diff --git a/SL/bus/chain/Chain.js b/SL/bus/chain/Chain.js new file mode 100644 index 0000000..66ae472 --- /dev/null +++ b/SL/bus/chain/Chain.js @@ -0,0 +1,56 @@ +/** + * 通用责任链/中间件管理器 + * 用于规范操作顺序,支持异步流程控制 + */ +export class Chain { + constructor() { + this.middlewares = []; + } + + /** + * 注册中间件 + * @param {Function} fn (context, next) => Promise | void + */ + use(fn) { + if (typeof fn !== 'function') { + throw new Error('[Chain] Middleware must be a function'); + } + this.middlewares.push(fn); + return this; + } + + /** + * 执行责任链 + * @param {Object} context 传递给中间件的上下文对象 + */ + async execute(context = {}) { + let index = -1; + + const dispatch = async (i) => { + if (i <= index) { + throw new Error('[Chain] next() called multiple times in one middleware'); + } + index = i; + + const fn = this.middlewares[i]; + if (!fn) return; // 链结束 + + try { + // 执行中间件,传入 context 和 next 函数 + await fn(context, () => dispatch(i + 1)); + } catch (err) { + console.error('[Chain] Middleware execution error:', err); + throw err; + } + }; + + await dispatch(0); + } + + /** + * 清空链 + */ + clear() { + this.middlewares = []; + } +} diff --git a/SL/bus/log/Logger.js b/SL/bus/log/Logger.js index 51d3703..9916a44 100644 --- a/SL/bus/log/Logger.js +++ b/SL/bus/log/Logger.js @@ -10,12 +10,12 @@ class Logger { static LOG_HEADER_ERROR = '[ERROR]'; static LOG_LEVEL_CODE = { - none: 0, - debug: 1 << 0, // 1 - info: 1 << 1, // 2 - warn: 1 << 2, // 4 - error: 1 << 3, // 8 - all: (1 << 4) - 1 // 15 + none: 0x0, // 0 + debug: 0x1, // 1 + info: 0x2, // 2 + warn: 0x4, // 4 + error: 0x8, // 8 + all: 0xF // 15 }; constructor() { @@ -139,23 +139,31 @@ class Logger { return this.globalLevel; } - log(plugin, origin, type, message, inFile = false) { - // 获取当前上下文生效的日志级别掩码 - const effectiveMask = this._getEffectiveLevelMask(plugin, origin); + /** + * 标准日志处理方法 (Core Processing) + * 统一处理过滤、格式化和输出,支持默认归属 Global + */ + process(plugin, origin, type, message, inFile = false) { + // 1. 默认归属处理 + const safePlugin = plugin || 'Global'; + const safeOrigin = origin || 'System'; + + // 2. 获取当前上下文生效的日志级别掩码 + const effectiveMask = this._getEffectiveLevelMask(safePlugin, safeOrigin); - // 获取当前日志类型的位码 + // 3. 获取当前日志类型的位码 const typeCode = Logger.LOG_LEVEL_CODE[type]; - // 级别筛选:位与运算结果为0则表示该级别未开启 + // 4. 级别筛选:位与运算结果为0则表示该级别未开启 if (typeCode === undefined || (effectiveMask & typeCode) === 0) { return; } const timestamp = new Date().toLocaleTimeString(); // 格式: [12:00:00] [PluginName::ClassName] [INFO]: message - const fullMessage = `[${timestamp}] [${plugin}::${origin}] [${type.toUpperCase()}]: ${message}`; + const fullMessage = `[${timestamp}] [${safePlugin}::${safeOrigin}] [${type.toUpperCase()}]: ${message}`; - // 1. Console Output + // 5. Console Output switch (type) { case 'debug': console.debug(fullMessage); @@ -174,7 +182,7 @@ class Logger { break; } - // 2. File Output (via FilePipe) + // 6. File Output (via FilePipe) if (inFile) { // Logger 自身也需要作为系统组件注册,获取写入权限 if (!this.sysBus) { @@ -196,6 +204,10 @@ class Logger { } } + log(plugin, origin, type, message, inFile = false) { + this.process(plugin, origin, type, message, inFile); + } + } // Ensure Amily2Bus namespace exists to prevent crash if loaded out of order