From 001fe01ace517f910b6cb371bac066d73781c906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 28 Jan 2026 15:07:06 +0800 Subject: [PATCH] Add plugin logger interface and update builtin plugin Introduces a PluginLogger interface and injects a plugin-specific logger into the plugin context for consistent logging. Refactors the builtin plugin to use the new logger instead of direct console calls. Updates napcat-types to version 0.0.9 in dependencies and lock files. --- .../napcat-onebot/network/plugin-manger.ts | 32 ++++++++++++++++++- packages/napcat-plugin-builtin/index.ts | 19 +++++------ packages/napcat-plugin-builtin/package.json | 2 +- packages/napcat-types/package.public.json | 2 +- pnpm-lock.yaml | 10 +++--- 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/packages/napcat-onebot/network/plugin-manger.ts b/packages/napcat-onebot/network/plugin-manger.ts index 99a26ef3..502cf1f6 100644 --- a/packages/napcat-onebot/network/plugin-manger.ts +++ b/packages/napcat-onebot/network/plugin-manger.ts @@ -54,6 +54,22 @@ export class NapCatConfig { export type PluginConfigSchema = PluginConfigItem[]; +/** + * 插件日志接口 - 简化的日志 API + */ +export interface PluginLogger { + /** 普通日志 */ + log (...args: any[]): void; + /** 调试日志 */ + debug (...args: any[]): void; + /** 信息日志 */ + info (...args: any[]): void; + /** 警告日志 */ + warn (...args: any[]): void; + /** 错误日志 */ + error (...args: any[]): void; +} + export interface NapCatPluginContext { core: NapCatCore; oneBot: NapCatOneBot11Adapter; @@ -65,6 +81,8 @@ export interface NapCatPluginContext { NapCatConfig: typeof NapCatConfig; adapterName: string; pluginManager: OB11PluginMangerAdapter; + /** 插件日志器 - 自动添加插件名称前缀 */ + logger: PluginLogger; } export interface PluginModule { @@ -331,6 +349,17 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter { const dataPath = path.join(this.pluginPath, plugin.dirname, 'data'); const configPath = path.join(dataPath, 'config.json'); + // Create plugin-specific logger with prefix + const pluginPrefix = `[Plugin: ${plugin.name}]`; + const coreLogger = this.logger; + const pluginLogger: PluginLogger = { + log: (...args: any[]) => coreLogger.log(pluginPrefix, ...args), + debug: (...args: any[]) => coreLogger.logDebug(pluginPrefix, ...args), + info: (...args: any[]) => coreLogger.log(pluginPrefix, ...args), + warn: (...args: any[]) => coreLogger.logWarn(pluginPrefix, ...args), + error: (...args: any[]) => coreLogger.logError(pluginPrefix, ...args), + }; + const context: NapCatPluginContext = { core: this.core, oneBot: this.obContext, @@ -341,7 +370,8 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter { configPath: configPath, NapCatConfig: NapCatConfig, adapterName: this.name, - pluginManager: this + pluginManager: this, + logger: pluginLogger }; plugin.context = context; // Store context on plugin object diff --git a/packages/napcat-plugin-builtin/index.ts b/packages/napcat-plugin-builtin/index.ts index 35c21fd3..2fc7c9f0 100644 --- a/packages/napcat-plugin-builtin/index.ts +++ b/packages/napcat-plugin-builtin/index.ts @@ -1,12 +1,12 @@ import type { ActionMap } from 'napcat-types/napcat-onebot/action/index'; import { EventType } from 'napcat-types/napcat-onebot/event/index'; -import type { PluginModule } from 'napcat-types/napcat-onebot/network/plugin-manger'; +import type { PluginModule, PluginLogger, PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger'; import type { OB11Message, OB11PostSendMsg } from 'napcat-types/napcat-onebot/types/index'; import fs from 'fs'; import path from 'path'; -import type { PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger'; import { NetworkAdapterConfig } from 'napcat-types/napcat-onebot/config/config'; let startTime: number = Date.now(); +let logger: PluginLogger | null = null; interface BuiltinPluginConfig { prefix: string; @@ -27,7 +27,8 @@ let currentConfig: BuiltinPluginConfig = { export let plugin_config_ui: PluginConfigSchema = []; const plugin_init: PluginModule['plugin_init'] = async (ctx) => { - console.log('[Plugin: builtin] NapCat 内置插件已初始化'); + logger = ctx.logger; + logger.info('NapCat 内置插件已初始化'); plugin_config_ui = ctx.NapCatConfig.combine( ctx.NapCatConfig.html('

👋 Welcome to NapCat Builtin Plugin

This is a demonstration of the plugin configuration interface.

'), ctx.NapCatConfig.text('prefix', 'Command Prefix', '#napcat', 'The prefix to trigger the version info command'), @@ -53,7 +54,7 @@ const plugin_init: PluginModule['plugin_init'] = async (ctx) => { Object.assign(currentConfig, savedConfig); } } catch (e) { - console.log('[Plugin: builtin] Failed to load config', e); + logger?.warn('Failed to load config', e); } }; @@ -73,7 +74,7 @@ export const plugin_set_config = async (ctx: any, config: BuiltinPluginConfig) = } fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8'); } catch (e) { - console.error('[Plugin: builtin] Failed to save config', e); + logger?.error('Failed to save config', e); throw e; } } @@ -95,9 +96,9 @@ const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) = const message = formatVersionMessage(versionInfo); await sendMessage(_ctx.actions, event, message, _ctx.adapterName, _ctx.pluginManager.config); - console.log('[Plugin: builtin] 已回复版本信息'); + logger?.info('已回复版本信息'); } catch (error) { - console.log('[Plugin: builtin] 处理消息时发生错误:', error); + logger?.error('处理消息时发生错误:', error); } }; @@ -112,7 +113,7 @@ async function getVersionInfo (actions: ActionMap, adapter: string, config: Netw protocolVersion: data.protocol_version, }; } catch (error) { - console.error('[Plugin: builtin] 获取版本信息失败:', error); + logger?.error('获取版本信息失败:', error); return null; } } @@ -150,7 +151,7 @@ async function sendMessage (actions: ActionMap, event: OB11Message, message: str try { await actions.call('send_msg', params, adapter, config); } catch (error) { - console.log('[Plugin: builtin] 发送消息失败:', error); + logger?.error('发送消息失败:', error); } } diff --git a/packages/napcat-plugin-builtin/package.json b/packages/napcat-plugin-builtin/package.json index 37b902f9..1b967b61 100644 --- a/packages/napcat-plugin-builtin/package.json +++ b/packages/napcat-plugin-builtin/package.json @@ -6,7 +6,7 @@ "description": "NapCat 内置插件", "author": "NapNeko", "dependencies": { - "napcat-types": "0.0.8" + "napcat-types": "0.0.9" }, "devDependencies": { "@types/node": "^22.0.1" diff --git a/packages/napcat-types/package.public.json b/packages/napcat-types/package.public.json index ad583299..3984acdb 100644 --- a/packages/napcat-types/package.public.json +++ b/packages/napcat-types/package.public.json @@ -1,6 +1,6 @@ { "name": "napcat-types", - "version": "0.0.8", + "version": "0.0.9", "private": false, "type": "module", "types": "./napcat-types/index.d.ts", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7040067a..29bc8c80 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -213,8 +213,8 @@ importers: packages/napcat-plugin-builtin: dependencies: napcat-types: - specifier: 0.0.8 - version: 0.0.8 + specifier: 0.0.9 + version: 0.0.9 devDependencies: '@types/node': specifier: ^22.0.1 @@ -5438,8 +5438,8 @@ packages: engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true - napcat-types@0.0.8: - resolution: {integrity: sha512-QxpYmAWr44tGimWmA6Az+eODlSOU3H2cJ9QzHvC09eaEwLvP6BpD0TApdWFYkzNBFFiHVLXv6lqdCAyAV3RAPg==} + napcat-types@0.0.9: + resolution: {integrity: sha512-lhK9SgGotQc58jobqnZEnDTadwcwT/Y62aGCH4hQijdiP2eSiu3YWX2oFFoUJxyIpUKFYAfWJkPUv1YHZ/aaWQ==} napcat.protobuf@1.1.4: resolution: {integrity: sha512-z7XtLSBJ/PxmYb0VD/w+eYr/X3LyGz+SZ2QejFTOczwt6zWNxy2yV1mTMTvJoc3BWkI3ESVFRxkuT6+pj1tb1Q==} @@ -12809,7 +12809,7 @@ snapshots: nanoid@3.3.11: {} - napcat-types@0.0.8: + napcat-types@0.0.9: dependencies: '@sinclair/typebox': 0.34.41 '@types/cors': 2.8.19