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.
This commit is contained in:
1 parent
6af66797dc
commit
96427376ec
5 files changed
+48
-17
No files matched your search
@@ -54,6 +54,22 @@ export class NapCatConfig {
|
|||||||
|
|
||||||
export type PluginConfigSchema = PluginConfigItem[];
|
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 {
|
export interface NapCatPluginContext {
|
||||||
core: NapCatCore;
|
core: NapCatCore;
|
||||||
oneBot: NapCatOneBot11Adapter;
|
oneBot: NapCatOneBot11Adapter;
|
||||||
@@ -65,6 +81,8 @@ export interface NapCatPluginContext {
|
|||||||
NapCatConfig: typeof NapCatConfig;
|
NapCatConfig: typeof NapCatConfig;
|
||||||
adapterName: string;
|
adapterName: string;
|
||||||
pluginManager: OB11PluginMangerAdapter;
|
pluginManager: OB11PluginMangerAdapter;
|
||||||
|
/** 插件日志器 - 自动添加插件名称前缀 */
|
||||||
|
logger: PluginLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PluginModule<T extends OB11EmitEventContent = OB11EmitEventContent> {
|
export interface PluginModule<T extends OB11EmitEventContent = OB11EmitEventContent> {
|
||||||
@@ -331,6 +349,17 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
|||||||
const dataPath = path.join(this.pluginPath, plugin.dirname, 'data');
|
const dataPath = path.join(this.pluginPath, plugin.dirname, 'data');
|
||||||
const configPath = path.join(dataPath, 'config.json');
|
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 = {
|
const context: NapCatPluginContext = {
|
||||||
core: this.core,
|
core: this.core,
|
||||||
oneBot: this.obContext,
|
oneBot: this.obContext,
|
||||||
@@ -341,7 +370,8 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
|||||||
configPath: configPath,
|
configPath: configPath,
|
||||||
NapCatConfig: NapCatConfig,
|
NapCatConfig: NapCatConfig,
|
||||||
adapterName: this.name,
|
adapterName: this.name,
|
||||||
pluginManager: this
|
pluginManager: this,
|
||||||
|
logger: pluginLogger
|
||||||
};
|
};
|
||||||
|
|
||||||
plugin.context = context; // Store context on plugin object
|
plugin.context = context; // Store context on plugin object
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import type { ActionMap } from 'napcat-types/napcat-onebot/action/index';
|
import type { ActionMap } from 'napcat-types/napcat-onebot/action/index';
|
||||||
import { EventType } from 'napcat-types/napcat-onebot/event/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 type { OB11Message, OB11PostSendMsg } from 'napcat-types/napcat-onebot/types/index';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import type { PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
|
||||||
import { NetworkAdapterConfig } from 'napcat-types/napcat-onebot/config/config';
|
import { NetworkAdapterConfig } from 'napcat-types/napcat-onebot/config/config';
|
||||||
let startTime: number = Date.now();
|
let startTime: number = Date.now();
|
||||||
|
let logger: PluginLogger | null = null;
|
||||||
|
|
||||||
interface BuiltinPluginConfig {
|
interface BuiltinPluginConfig {
|
||||||
prefix: string;
|
prefix: string;
|
||||||
@@ -27,7 +27,8 @@ let currentConfig: BuiltinPluginConfig = {
|
|||||||
export let plugin_config_ui: PluginConfigSchema = [];
|
export let plugin_config_ui: PluginConfigSchema = [];
|
||||||
|
|
||||||
const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
|
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(
|
plugin_config_ui = ctx.NapCatConfig.combine(
|
||||||
ctx.NapCatConfig.html('<div style="padding: 10px; background: rgba(0,0,0,0.05); border-radius: 8px;"><h3>👋 Welcome to NapCat Builtin Plugin</h3><p>This is a demonstration of the plugin configuration interface.</p></div>'),
|
ctx.NapCatConfig.html('<div style="padding: 10px; background: rgba(0,0,0,0.05); border-radius: 8px;"><h3>👋 Welcome to NapCat Builtin Plugin</h3><p>This is a demonstration of the plugin configuration interface.</p></div>'),
|
||||||
ctx.NapCatConfig.text('prefix', 'Command Prefix', '#napcat', 'The prefix to trigger the version info command'),
|
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);
|
Object.assign(currentConfig, savedConfig);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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');
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[Plugin: builtin] Failed to save config', e);
|
logger?.error('Failed to save config', e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,9 +96,9 @@ const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) =
|
|||||||
const message = formatVersionMessage(versionInfo);
|
const message = formatVersionMessage(versionInfo);
|
||||||
await sendMessage(_ctx.actions, event, message, _ctx.adapterName, _ctx.pluginManager.config);
|
await sendMessage(_ctx.actions, event, message, _ctx.adapterName, _ctx.pluginManager.config);
|
||||||
|
|
||||||
console.log('[Plugin: builtin] 已回复版本信息');
|
logger?.info('已回复版本信息');
|
||||||
} catch (error) {
|
} 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,
|
protocolVersion: data.protocol_version,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Plugin: builtin] 获取版本信息失败:', error);
|
logger?.error('获取版本信息失败:', error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,7 +151,7 @@ async function sendMessage (actions: ActionMap, event: OB11Message, message: str
|
|||||||
try {
|
try {
|
||||||
await actions.call('send_msg', params, adapter, config);
|
await actions.call('send_msg', params, adapter, config);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('[Plugin: builtin] 发送消息失败:', error);
|
logger?.error('发送消息失败:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
"description": "NapCat 内置插件",
|
"description": "NapCat 内置插件",
|
||||||
"author": "NapNeko",
|
"author": "NapNeko",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"napcat-types": "0.0.8"
|
"napcat-types": "0.0.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.0.1"
|
"@types/node": "^22.0.1"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "napcat-types",
|
"name": "napcat-types",
|
||||||
"version": "0.0.8",
|
"version": "0.0.9",
|
||||||
"private": false,
|
"private": false,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"types": "./napcat-types/index.d.ts",
|
"types": "./napcat-types/index.d.ts",
|
||||||
|
|||||||
Generated
+5
-5
@@ -213,8 +213,8 @@ importers:
|
|||||||
packages/napcat-plugin-builtin:
|
packages/napcat-plugin-builtin:
|
||||||
dependencies:
|
dependencies:
|
||||||
napcat-types:
|
napcat-types:
|
||||||
specifier: 0.0.8
|
specifier: 0.0.9
|
||||||
version: 0.0.8
|
version: 0.0.9
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: ^22.0.1
|
specifier: ^22.0.1
|
||||||
@@ -5438,8 +5438,8 @@ packages:
|
|||||||
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
|
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
napcat-types@0.0.8:
|
napcat-types@0.0.9:
|
||||||
resolution: {integrity: sha512-QxpYmAWr44tGimWmA6Az+eODlSOU3H2cJ9QzHvC09eaEwLvP6BpD0TApdWFYkzNBFFiHVLXv6lqdCAyAV3RAPg==}
|
resolution: {integrity: sha512-lhK9SgGotQc58jobqnZEnDTadwcwT/Y62aGCH4hQijdiP2eSiu3YWX2oFFoUJxyIpUKFYAfWJkPUv1YHZ/aaWQ==}
|
||||||
|
|
||||||
napcat.protobuf@1.1.4:
|
napcat.protobuf@1.1.4:
|
||||||
resolution: {integrity: sha512-z7XtLSBJ/PxmYb0VD/w+eYr/X3LyGz+SZ2QejFTOczwt6zWNxy2yV1mTMTvJoc3BWkI3ESVFRxkuT6+pj1tb1Q==}
|
resolution: {integrity: sha512-z7XtLSBJ/PxmYb0VD/w+eYr/X3LyGz+SZ2QejFTOczwt6zWNxy2yV1mTMTvJoc3BWkI3ESVFRxkuT6+pj1tb1Q==}
|
||||||
@@ -12809,7 +12809,7 @@ snapshots:
|
|||||||
|
|
||||||
nanoid@3.3.11: {}
|
nanoid@3.3.11: {}
|
||||||
|
|
||||||
napcat-types@0.0.8:
|
napcat-types@0.0.9:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@sinclair/typebox': 0.34.41
|
'@sinclair/typebox': 0.34.41
|
||||||
'@types/cors': 2.8.19
|
'@types/cors': 2.8.19
|
||||||
|
|||||||
Reference in new issue
Block a user