Skip to content

Commit 27ae546

Browse files
committed
fix composite logger not redacting correctly
1 parent e3507e5 commit 27ae546

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/common/logger.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,19 @@ interface LogPayload {
6060
export type LoggerType = "console" | "disk" | "mcp";
6161

6262
export abstract class LoggerBase {
63-
abstract log(level: LogLevel, payload: Omit<LogPayload, "noRedaction">): void;
64-
65-
abstract type: LoggerType | null;
66-
67-
private logCore(level: LogLevel, payload: LogPayload) {
68-
// Default to not redacting mcp logs, redact everything else
63+
public log(level: LogLevel, payload: LogPayload): void {
6964
const noRedaction = payload.noRedaction !== undefined ? payload.noRedaction : "mcp";
7065

71-
this.log(level, {
72-
id: payload.id,
73-
context: payload.context,
66+
this.logCore(level, {
67+
...payload,
7468
message: this.redactIfNecessary(payload.message, noRedaction),
7569
});
7670
}
7771

72+
protected abstract type: LoggerType;
73+
74+
protected abstract logCore(level: LogLevel, payload: LogPayload): void;
75+
7876
private redactIfNecessary(message: string, noRedaction: LogPayload["noRedaction"]): string {
7977
if (typeof noRedaction === "boolean" && noRedaction) {
8078
// If the consumer has supplied noRedaction: true, we don't redact the log message
@@ -135,9 +133,9 @@ export abstract class LoggerBase {
135133
}
136134

137135
export class ConsoleLogger extends LoggerBase {
138-
type: LoggerType = "console";
136+
protected type: LoggerType = "console";
139137

140-
log(level: LogLevel, payload: LogPayload): void {
138+
protected logCore(level: LogLevel, payload: LogPayload): void {
141139
const { id, context, message } = payload;
142140
console.error(`[${level.toUpperCase()}] ${id.__value} - ${context}: ${message} (${process.pid})`);
143141
}
@@ -148,7 +146,7 @@ export class DiskLogger extends LoggerBase {
148146
super();
149147
}
150148

151-
type: LoggerType = "disk";
149+
protected type: LoggerType = "disk";
152150

153151
static async fromPath(logPath: string): Promise<DiskLogger> {
154152
await fs.mkdir(logPath, { recursive: true });
@@ -169,7 +167,7 @@ export class DiskLogger extends LoggerBase {
169167
return new DiskLogger(logWriter);
170168
}
171169

172-
log(level: LogLevel, payload: LogPayload): void {
170+
protected logCore(level: LogLevel, payload: LogPayload): void {
173171
const { id, context, message } = payload;
174172
const mongoDBLevel = this.mapToMongoDBLogLevel(level);
175173

@@ -204,7 +202,7 @@ export class McpLogger extends LoggerBase {
204202

205203
type: LoggerType = "mcp";
206204

207-
log(level: LogLevel, payload: LogPayload): void {
205+
protected logCore(level: LogLevel, payload: LogPayload): void {
208206
// Only log if the server is connected
209207
if (!this.server?.isConnected()) {
210208
return;
@@ -218,7 +216,8 @@ export class McpLogger extends LoggerBase {
218216
}
219217

220218
class CompositeLogger extends LoggerBase {
221-
type: LoggerType | null = null;
219+
// This is not a real logger type - it should not be used anyway.
220+
protected type: LoggerType = "composite" as unknown as LoggerType;
222221

223222
private loggers: LoggerBase[] = [];
224223

@@ -235,11 +234,16 @@ class CompositeLogger extends LoggerBase {
235234
this.loggers = [...loggers];
236235
}
237236

238-
log(level: LogLevel, payload: LogPayload): void {
237+
public log(level: LogLevel, payload: LogPayload): void {
238+
// Override the public method to avoid the base logger redacting the message payload
239239
for (const logger of this.loggers) {
240240
logger.log(level, payload);
241241
}
242242
}
243+
244+
protected logCore(): void {
245+
throw new Error("logCore should never be invoked on CompositeLogger");
246+
}
243247
}
244248

245249
const logger = new CompositeLogger(new ConsoleLogger());

0 commit comments

Comments
 (0)