Spaces:
Sleeping
Sleeping
File size: 1,707 Bytes
7dc28be | 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 | // src/logger.ts
//
// Centralized logger with LOG_LEVEL support.
//
// Log levels (from most to least verbose):
// debug - Detailed diagnostic info (text range mapping, request building, etc.)
// info - General operational messages (server started, auth succeeded, etc.)
// warn - Potentially harmful situations (missing content, fallback behavior)
// error - Error conditions (API failures, auth failures, etc.)
//
// Set via the LOG_LEVEL environment variable. Defaults to "info".
// Example: LOG_LEVEL=debug npm start
//
// MCP servers communicate over stdout, so all log output goes to stderr.
const LOG_LEVELS = {
debug: 0,
info: 1,
warn: 2,
error: 3,
silent: 4,
} as const;
type LogLevel = keyof typeof LOG_LEVELS;
function resolveLevel(): LogLevel {
const env = process.env.LOG_LEVEL?.toLowerCase();
if (env && env in LOG_LEVELS) {
return env as LogLevel;
}
return 'info';
}
let currentLevel = resolveLevel();
/** Re-read LOG_LEVEL from the environment (useful for testing). */
export function refreshLogLevel(): void {
currentLevel = resolveLevel();
}
function shouldLog(level: LogLevel): boolean {
return LOG_LEVELS[level] >= LOG_LEVELS[currentLevel];
}
export const logger = {
debug(...args: unknown[]): void {
if (shouldLog('debug')) {
console.error('[DEBUG]', ...args);
}
},
info(...args: unknown[]): void {
if (shouldLog('info')) {
console.error('[INFO]', ...args);
}
},
warn(...args: unknown[]): void {
if (shouldLog('warn')) {
console.error('[WARN]', ...args);
}
},
error(...args: unknown[]): void {
if (shouldLog('error')) {
console.error('[ERROR]', ...args);
}
},
};
|