UNPKG

smart-thinking-mcp

Version:

Un serveur MCP avancé pour le raisonnement multi-dimensionnel, adaptatif et collaboratif

137 lines 3.86 kB
"use strict"; /** * Logger utilitaire minimaliste avec niveaux configurables et sortie structurée. * S'aligne sur les bonnes pratiques de journalisation Node.js : niveaux, structure JSON, * et contrôle par variables d'environnement. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = void 0; exports.setLogLevel = setLogLevel; exports.setLogFormat = setLogFormat; const LEVELS = { silent: 0, error: 1, warn: 2, info: 3, debug: 4, }; const GLOBAL_SYMBOL = Symbol.for('smart-thinking.logger.initialized'); let currentLevel = resolveInitialLevel(); let useJsonFormat = resolveInitialFormat(); function resolveInitialLevel() { const envLevel = (process.env.SMART_THINKING_LOG_LEVEL || '').toLowerCase(); if (envLevel && envLevel in LEVELS) { return envLevel; } return process.env.NODE_ENV === 'production' ? 'info' : 'debug'; } function resolveInitialFormat() { const format = (process.env.SMART_THINKING_LOG_FORMAT || '').toLowerCase(); if (format === 'json') { return true; } if (format === 'pretty') { return false; } return process.env.NODE_ENV === 'production'; } function shouldLog(level) { return LEVELS[level] <= LEVELS[currentLevel]; } function formatArgs(args) { if (args.length === 1 && typeof args[0] === 'string') { return args[0]; } return args .map((arg) => { if (typeof arg === 'string') { return arg; } if (arg instanceof Error) { return `${arg.name}: ${arg.message}\n${arg.stack}`; } try { return JSON.stringify(arg); } catch { return String(arg); } }) .join(' '); } function getOrigin() { const error = new Error(); if (!error.stack) { return undefined; } const stackLines = error.stack.split('\n').slice(3); for (const line of stackLines) { const trimmed = line.trim(); if (!trimmed.includes('logger.ts')) { return trimmed; } } return undefined; } function write(payload, stream) { if (useJsonFormat) { stream.write(`${JSON.stringify(payload)}\n`); return; } const contextInfo = payload.origin ? ` (${payload.origin})` : ''; stream.write(`[${payload.timestamp}] ${payload.level.toUpperCase()}${contextInfo}: ${payload.message}\n`); } function buildConsoleMethod(level) { const stream = process.stderr; return (...args) => { if (!shouldLog(level)) { return; } const payload = { level, timestamp: new Date().toISOString(), message: formatArgs(args), origin: getOrigin(), }; write(payload, stream); }; } function patchConsole() { if (global[GLOBAL_SYMBOL]) { return; } global[GLOBAL_SYMBOL] = true; const originalConsole = { ...console }; console.error = buildConsoleMethod('error'); console.warn = buildConsoleMethod('warn'); console.info = buildConsoleMethod('info'); console.log = buildConsoleMethod('info'); console.debug = buildConsoleMethod('debug'); console.trace = ((...args) => { if (!shouldLog('debug')) { return; } originalConsole.trace(...args); }); } /** * Permet de modifier dynamiquement le niveau de log. */ function setLogLevel(level) { if (!(level in LEVELS)) { throw new Error(`Niveau de log inconnu: ${level}`); } currentLevel = level; } /** * Permet de basculer entre format JSON (production) et format texte lisible. */ function setLogFormat(format) { useJsonFormat = format === 'json'; } patchConsole(); exports.Logger = { setLevel: setLogLevel, setFormat: setLogFormat, }; //# sourceMappingURL=logger.js.map