|
| 1 | +/** |
| 2 | + * Internal logging infrastructure for the mcpd SDK. |
| 3 | + * |
| 4 | + * This module provides a logging shim controlled by the MCPD_LOG_LEVEL environment |
| 5 | + * variable. |
| 6 | + * |
| 7 | + * Logging is disabled by default. |
| 8 | + * |
| 9 | + * NOTE: It is recommended that you only enable MCPD_LOG_LEVEL in non-MCP-server contexts. |
| 10 | + * MCP servers using stdio transport for JSON-RPC communication should avoid enabling logging |
| 11 | + * to avoid contaminating stdout/stderr. |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Valid log levels for MCPD_LOG_LEVEL environment variable. |
| 16 | + */ |
| 17 | +export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "off"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Logger interface defining the SDK's logging contract. |
| 21 | + * |
| 22 | + * Custom loggers must implement all methods. Each method accepts a message |
| 23 | + * and optional arguments for string formatting. |
| 24 | + */ |
| 25 | +export interface Logger { |
| 26 | + /** |
| 27 | + * Log a trace-level message (most verbose). |
| 28 | + * |
| 29 | + * @param args - Message and optional formatting arguments. |
| 30 | + */ |
| 31 | + trace(...args: unknown[]): void; |
| 32 | + |
| 33 | + /** |
| 34 | + * Log a debug-level message. |
| 35 | + * |
| 36 | + * @param args - Message and optional formatting arguments. |
| 37 | + */ |
| 38 | + debug(...args: unknown[]): void; |
| 39 | + |
| 40 | + /** |
| 41 | + * Log an info-level message. |
| 42 | + * |
| 43 | + * @param args - Message and optional formatting arguments. |
| 44 | + */ |
| 45 | + info(...args: unknown[]): void; |
| 46 | + |
| 47 | + /** |
| 48 | + * Log a warning-level message. |
| 49 | + * |
| 50 | + * @param args - Message and optional formatting arguments. |
| 51 | + */ |
| 52 | + warn(...args: unknown[]): void; |
| 53 | + |
| 54 | + /** |
| 55 | + * Log an error-level message. |
| 56 | + * |
| 57 | + * @param args - Message and optional formatting arguments. |
| 58 | + */ |
| 59 | + error(...args: unknown[]): void; |
| 60 | +} |
| 61 | + |
| 62 | +// Numeric ranks for log levels (lower = more verbose). |
| 63 | +const ranks: Record<LogLevel, number> = { |
| 64 | + trace: 5, |
| 65 | + debug: 10, |
| 66 | + info: 20, |
| 67 | + warn: 30, |
| 68 | + error: 40, |
| 69 | + off: 1000, |
| 70 | +}; |
| 71 | + |
| 72 | +// Attempts to resolve log level from a (case insensitive) environment variable value. |
| 73 | +// Defaults to "off" if unrecognized. |
| 74 | +function resolve(raw: string | undefined): LogLevel { |
| 75 | + const candidate = raw?.toLowerCase() as LogLevel | undefined; |
| 76 | + return candidate && candidate in ranks ? candidate : "off"; |
| 77 | +} |
| 78 | + |
| 79 | +// Lazily resolve the level at call time to support testing. |
| 80 | +function getLevel(): LogLevel { |
| 81 | + return resolve( |
| 82 | + typeof process !== "undefined" ? process.env.MCPD_LOG_LEVEL : undefined, |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +// Default logger implementation using console methods. |
| 87 | +function defaultLogger(): Logger { |
| 88 | + const OFF: LogLevel = "off"; |
| 89 | + |
| 90 | + return { |
| 91 | + trace: (...args) => { |
| 92 | + const lvl = getLevel(); |
| 93 | + if (lvl !== OFF && ranks[lvl] <= ranks.trace) console.trace(...args); |
| 94 | + }, |
| 95 | + debug: (...args) => { |
| 96 | + const lvl = getLevel(); |
| 97 | + if (lvl !== OFF && ranks[lvl] <= ranks.debug) console.debug(...args); |
| 98 | + }, |
| 99 | + info: (...args) => { |
| 100 | + const lvl = getLevel(); |
| 101 | + if (lvl !== OFF && ranks[lvl] <= ranks.info) console.info(...args); |
| 102 | + }, |
| 103 | + warn: (...args) => { |
| 104 | + const lvl = getLevel(); |
| 105 | + if (lvl !== OFF && ranks[lvl] <= ranks.warn) console.warn(...args); |
| 106 | + }, |
| 107 | + error: (...args) => { |
| 108 | + const lvl = getLevel(); |
| 109 | + if (lvl !== OFF && ranks[lvl] <= ranks.error) console.error(...args); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Create a logger, optionally using a custom implementation. |
| 116 | + * |
| 117 | + * This function allows SDK users to inject their own logger implementation. |
| 118 | + * Supports partial implementations - any omitted methods will fall back to the |
| 119 | + * default logger, which respects the MCPD_LOG_LEVEL environment variable. |
| 120 | + * |
| 121 | + * @param impl - Optional custom Logger implementation or partial implementation. |
| 122 | + * If not provided, uses default logger controlled by MCPD_LOG_LEVEL. |
| 123 | + * If partially provided, custom methods are used and omitted methods |
| 124 | + * fall back to default logger (which respects MCPD_LOG_LEVEL). |
| 125 | + * @returns A Logger instance with all methods implemented. |
| 126 | + * |
| 127 | + * @example |
| 128 | + * ```typescript |
| 129 | + * // Use default logger (controlled by MCPD_LOG_LEVEL). |
| 130 | + * const logger = createLogger(); |
| 131 | + * |
| 132 | + * // Partial logger: custom warn/error, default (MCPD_LOG_LEVEL-aware) for others. |
| 133 | + * const logger = createLogger({ |
| 134 | + * warn: (msg) => myCustomLogger.warning(msg), |
| 135 | + * error: (msg) => myCustomLogger.error(msg), |
| 136 | + * // trace, debug, info fall back to default logger (respects MCPD_LOG_LEVEL) |
| 137 | + * }); |
| 138 | + * ``` |
| 139 | + */ |
| 140 | +export function createLogger(impl?: Partial<Logger>): Logger { |
| 141 | + const base = defaultLogger(); |
| 142 | + return { |
| 143 | + trace: impl?.trace ?? base.trace, |
| 144 | + debug: impl?.debug ?? base.debug, |
| 145 | + info: impl?.info ?? base.info, |
| 146 | + warn: impl?.warn ?? base.warn, |
| 147 | + error: impl?.error ?? base.error, |
| 148 | + }; |
| 149 | +} |
0 commit comments