Skip to content

Commit 0b129f3

Browse files
author
linyuan.yang
committed
channel 日志加前缀
1 parent f5f1f34 commit 0b129f3

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎packages/backend/sbot/src/Channel/ChannelManager.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TimeUtils, formatError } from "scorpio.ai";
44
import { Op } from "sequelize";
55
import { sessionManager } from "../Session/SessionManager";
66
import { channelDataService } from "../Session/ChannelDataService";
7-
import { LoggerService } from "../Core/LoggerService";
7+
import { LoggerService, PrefixedLogger } from "../Core/LoggerService";
88
import { config } from "../Core/Config";
99
import { compareSemver, fetchLatestRelease, WEB_CHANNEL_ID, WEB_CHANNEL_TYPE } from "@sbot/shared";
1010
import { PluginLoader } from "./PluginLoader";
@@ -137,10 +137,12 @@ export class ChannelManager {
137137
}
138138
const name = channel.name ? `${channel.name} (${channelId})` : channelId;
139139
const label = `[${name}] (${plugin.type})`;
140+
// 为每个 channel 实例提供独立前缀的 logger,避免多实例日志混淆(含注入 larkClient 等渠道 SDK 的日志)
141+
const pluginLogger = new PrefixedLogger(logger, name);
140142
try {
141143
const ctx: ChannelPluginContext = {
142144
config: channel.config ?? {},
143-
logger,
145+
logger: pluginLogger,
144146
filterEvent,
145147
initSession: async (initCtx) => {
146148
const { dbUserId, dbSessionId } = await doInitSession(channelId, initCtx);

‎packages/backend/sbot/src/Core/LoggerService.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ class Log4jsLoggerAdapter implements ILogger {
6161
}
6262
}
6363

64+
/**
65+
* 带前缀的 Logger 包装器,实现 ILogger 接口
66+
* 用于多实例场景(如每个 channel 插件)区分日志来源
67+
*/
68+
export class PrefixedLogger implements ILogger {
69+
constructor(private logger: ILogger, private prefix: string) {}
70+
71+
debug(message: string, ...args: any[]): void {
72+
this.logger.debug(`[${this.prefix}] ${message}`, ...args);
73+
}
74+
75+
info(message: string, ...args: any[]): void {
76+
this.logger.info(`[${this.prefix}] ${message}`, ...args);
77+
}
78+
79+
warn(message: string, ...args: any[]): void {
80+
this.logger.warn(`[${this.prefix}] ${message}`, ...args);
81+
}
82+
83+
error(message: string, ...args: any[]): void {
84+
this.logger.error(`[${this.prefix}] ${message}`, ...args);
85+
}
86+
}
87+
6488
/**
6589
* Log4js LoggerService 实现
6690
*/

0 commit comments

Comments
 (0)