UNPKG

bc_resource_mcp

Version:

MCP server for Baichuan resource

80 lines (79 loc) 2.9 kB
export var LogLevel; (function (LogLevel) { LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG"; LogLevel[LogLevel["INFO"] = 1] = "INFO"; LogLevel[LogLevel["WARN"] = 2] = "WARN"; LogLevel[LogLevel["ERROR"] = 3] = "ERROR"; })(LogLevel || (LogLevel = {})); export class Logger { static level = LogLevel.INFO; static timestamp = true; static setLevel(level) { this.level = level; } static getLevel() { return this.level; } static enableTimestamp(enable) { this.timestamp = enable; } static getTimestamp() { return this.timestamp ? `[${new Date().toISOString()}] ` : ""; } static formatArgs(args) { return args.map((arg) => { if (typeof arg === "object" && arg !== null) { try { // 使用更安全的方式处理对象 if (arg instanceof Error) { // 特殊处理 Error 对象 return { message: arg.message, name: arg.name, stack: arg.stack, }; } // 处理循环引用问题 const cache = new Set(); const safeStringify = (obj) => { return JSON.stringify(obj, (key, value) => { if (typeof value === "object" && value !== null) { if (cache.has(value)) { return "[循环引用]"; } cache.add(value); } return value; }, 2); }; return safeStringify(arg); } catch (e) { return `[无法序列化的对象: ${typeof arg}]`; } } return arg; }); } static debug(message, ...args) { if (this.level <= LogLevel.DEBUG) { console.debug(`${this.getTimestamp()}${message}`, ...this.formatArgs(args)); } } static info(message, ...args) { if (this.level <= LogLevel.INFO) { // WARN: Claude 客户端不支持 console.log console.info 方法,所以这里使用 console.error 代替 console.error(`${this.getTimestamp()}${message}`, ...this.formatArgs(args)); } } static warn(message, ...args) { if (this.level <= LogLevel.WARN) { console.warn(`${this.getTimestamp()}${message}`, ...this.formatArgs(args)); } } static error(message, ...args) { if (this.level <= LogLevel.ERROR) { console.error(`${this.getTimestamp()}${message}`, ...this.formatArgs(args)); } } }