mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 15:05:51 +00:00
初步实现Logger功能
This commit is contained in:
@@ -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 插件名称
|
||||
|
||||
56
SL/bus/chain/Chain.js
Normal file
56
SL/bus/chain/Chain.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 通用责任链/中间件管理器
|
||||
* 用于规范操作顺序,支持异步流程控制
|
||||
*/
|
||||
export class Chain {
|
||||
constructor() {
|
||||
this.middlewares = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册中间件
|
||||
* @param {Function} fn (context, next) => Promise<void> | 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 = [];
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user