forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
169 lines (138 loc) · 4.1 KB
/
logger.ts
File metadata and controls
169 lines (138 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Centralized logging for MCP UI operations.
* Provides structured, contextual logs with levels.
*/
export type LogLevel = "debug" | "info" | "warn" | "error";
export interface LogContext {
server?: string;
session?: string;
tool?: string;
uri?: string;
[key: string]: unknown;
}
export interface LogEntry {
level: LogLevel;
message: string;
context?: LogContext;
error?: Error;
timestamp: Date;
}
type LogHandler = (entry: LogEntry) => void;
const LEVEL_PRIORITY: Record<LogLevel, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3,
};
const LEVEL_PREFIX: Record<LogLevel, string> = {
debug: "[MCP-UI:DEBUG]",
info: "[MCP-UI]",
warn: "[MCP-UI:WARN]",
error: "[MCP-UI:ERROR]",
};
class Logger {
private minLevel: LogLevel = "info";
private handlers: LogHandler[] = [];
private defaultContext: LogContext = {};
setLevel(level: LogLevel): void {
this.minLevel = level;
}
setDefaultContext(context: LogContext): void {
this.defaultContext = context;
}
addHandler(handler: LogHandler): void {
this.handlers.push(handler);
}
clearHandlers(): void {
this.handlers = [];
}
private shouldLog(level: LogLevel): boolean {
return LEVEL_PRIORITY[level] >= LEVEL_PRIORITY[this.minLevel];
}
private emit(level: LogLevel, message: string, context?: LogContext, error?: Error): void {
if (!this.shouldLog(level)) return;
const entry: LogEntry = {
level,
message,
context: { ...this.defaultContext, ...context },
error,
timestamp: new Date(),
};
// Default console output
const prefix = LEVEL_PREFIX[level];
const contextStr = formatContext(entry.context);
const fullMessage = contextStr ? `${prefix} ${message} ${contextStr}` : `${prefix} ${message}`;
if (level === "error") {
console.error(fullMessage, error ?? "");
} else if (level === "warn") {
console.warn(fullMessage);
} else if (level === "debug") {
console.debug(fullMessage);
} else {
console.log(fullMessage);
}
// Custom handlers
for (const handler of this.handlers) {
try {
handler(entry);
} catch {
// Ignore handler errors
}
}
}
debug(message: string, context?: LogContext): void {
this.emit("debug", message, context);
}
info(message: string, context?: LogContext): void {
this.emit("info", message, context);
}
warn(message: string, context?: LogContext): void {
this.emit("warn", message, context);
}
error(message: string, error?: Error, context?: LogContext): void {
this.emit("error", message, context, error);
}
/**
* Create a child logger with additional default context.
*/
child(context: LogContext): ChildLogger {
return new ChildLogger(this, context);
}
}
class ChildLogger {
constructor(
private parent: Logger,
private context: LogContext
) {}
debug(message: string, context?: LogContext): void {
this.parent.debug(message, { ...this.context, ...context });
}
info(message: string, context?: LogContext): void {
this.parent.info(message, { ...this.context, ...context });
}
warn(message: string, context?: LogContext): void {
this.parent.warn(message, { ...this.context, ...context });
}
error(message: string, error?: Error, context?: LogContext): void {
this.parent.error(message, error, { ...this.context, ...context });
}
child(context: LogContext): ChildLogger {
return new ChildLogger(this.parent, { ...this.context, ...context });
}
}
function formatContext(context?: LogContext): string {
if (!context || Object.keys(context).length === 0) return "";
const parts: string[] = [];
for (const [key, value] of Object.entries(context)) {
if (value !== undefined && value !== null) {
parts.push(`${key}=${typeof value === "string" ? value : JSON.stringify(value)}`);
}
}
return parts.length > 0 ? `(${parts.join(", ")})` : "";
}
// Singleton instance
export const logger = new Logger();
// Enable debug mode via environment variable
if (process.env.MCP_UI_DEBUG === "1" || process.env.MCP_UI_DEBUG === "true") {
logger.setLevel("debug");
}