UNPKG

@turbot/tailpipe-mcp

Version:

Tailpipe MCP server to query cloud and security logs using AI.

121 lines 3.61 kB
/** * Logger utility for Tailpipe MCP * * Uses stderr for all output to avoid interfering with MCP protocol, * and supports suppressing logs during tests. */ // Define log levels export var LogLevel; (function (LogLevel) { LogLevel["DEBUG"] = "debug"; LogLevel["INFO"] = "info"; LogLevel["WARN"] = "warn"; LogLevel["ERROR"] = "error"; LogLevel["SILENT"] = "silent"; })(LogLevel || (LogLevel = {})); export class Logger { options; logs = []; // Store logs when in test mode constructor(options = {}) { const envLevel = (process.env.TAILPIPE_MCP_LOG_LEVEL || 'info').toLowerCase(); this.options = { level: options.level || (this.isValidLogLevel(envLevel) ? envLevel : LogLevel.INFO), isTestEnvironment: options.isTestEnvironment || false }; } isValidLogLevel(level) { return Object.values(LogLevel).includes(level); } /** * Configure the logger */ configure(options) { this.options = { ...this.options, ...options }; } shouldLog(targetLevel) { const levels = [LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR, LogLevel.SILENT]; const currentLevelIndex = levels.indexOf(this.options.level); const targetLevelIndex = levels.indexOf(targetLevel); return currentLevelIndex <= targetLevelIndex && this.options.level !== LogLevel.SILENT; } /** * Debug level logging */ debug(message, ...args) { if (this.shouldLog(LogLevel.DEBUG)) { this.log('DEBUG', message, ...args); } } /** * Info level logging */ info(message, ...args) { if (this.shouldLog(LogLevel.INFO)) { this.log('INFO', message, ...args); } } /** * Warning level logging */ warn(message, ...args) { if (this.shouldLog(LogLevel.WARN)) { this.log('WARN', message, ...args); } } /** * Error level logging */ error(message, ...args) { if (this.shouldLog(LogLevel.ERROR)) { this.log('ERROR', message, ...args); } } /** * Internal log function */ log(level, message, ...args) { const timestamp = new Date().toISOString(); let formattedMessage = `[${timestamp}] [${level}] ${message}`; // Format additional arguments if present if (args.length > 0) { const formattedArgs = args.map(arg => { if (arg instanceof Error) { return arg.message; } else if (typeof arg === 'object') { try { return JSON.stringify(arg); } catch (e) { return String(arg); } } return String(arg); }); formattedMessage += `: ${formattedArgs.join(' ')}`; } // In test environment, store logs instead of printing if (this.options.isTestEnvironment) { this.logs.push(formattedMessage); } else { // Always use stderr to avoid interfering with MCP protocol console.error(formattedMessage); } } /** * Get collected logs (useful for tests) */ getCollectedLogs() { return [...this.logs]; } /** * Clear collected logs */ clearLogs() { this.logs = []; } } // Export singleton instance export const logger = new Logger(); //# sourceMappingURL=logger.js.map