|
| 1 | +import { config } from './config.js'; |
| 2 | +// Define log levels |
| 3 | +export enum LogLevel { |
| 4 | + DEBUG = 'debug', |
| 5 | + INFO = 'info', |
| 6 | + WARN = 'warn', |
| 7 | + ERROR = 'error', |
| 8 | +} |
| 9 | + |
| 10 | +// Map log levels to numeric values for comparison |
| 11 | +const logLevelValues: Record<LogLevel, number> = { |
| 12 | + [LogLevel.DEBUG]: 0, |
| 13 | + [LogLevel.INFO]: 1, |
| 14 | + [LogLevel.WARN]: 2, |
| 15 | + [LogLevel.ERROR]: 3, |
| 16 | +}; |
| 17 | + |
| 18 | +// Sensitive field patterns to redact |
| 19 | +const SENSITIVE_PATTERNS = [ |
| 20 | + /authorization/i, |
| 21 | + /secret/i, |
| 22 | + /token/i, |
| 23 | + /api[_-]?key/i, |
| 24 | + /bearer/i, |
| 25 | + /credential/i, |
| 26 | + /secret[_-]?key/i, |
| 27 | + /cvv/i, |
| 28 | + /number/i, |
| 29 | +]; |
| 30 | +/** |
| 31 | + * Redact sensitive fields in card objects |
| 32 | + * Only redacts cvv and number, keeps other fields visible |
| 33 | + */ |
| 34 | +function redactCardObject(card: any): any { |
| 35 | + if (Array.isArray(card)) { |
| 36 | + return card.map(redactCardObject); |
| 37 | + } |
| 38 | + |
| 39 | + if (typeof card === 'object' && card !== null) { |
| 40 | + const redactedCard: any = {}; |
| 41 | + for (const [key, value] of Object.entries(card)) { |
| 42 | + // Only redact cvv and number fields in card object |
| 43 | + if (key === 'cvv' || key === 'number') { |
| 44 | + redactedCard[key] = '[REDACTED]'; |
| 45 | + } else { |
| 46 | + // Keep other card fields but recursively redact if they're objects |
| 47 | + redactedCard[key] = redactSensitiveData(value); |
| 48 | + } |
| 49 | + } |
| 50 | + return redactedCard; |
| 51 | + } |
| 52 | + |
| 53 | + return card; |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +function redactSensitiveData(obj: any): any { |
| 58 | + if (obj === null || obj === undefined) { |
| 59 | + return obj; |
| 60 | + } |
| 61 | + |
| 62 | + if (typeof obj === 'string') { |
| 63 | + // Redact bearer tokens and API keys in strings |
| 64 | + return obj.replace(/Bearer\s+\w+/gi, 'Bearer [REDACTED]') |
| 65 | + .replace(/sk_test_\w+/g, '[REDACTED_SECRET_KEY]'); |
| 66 | + } |
| 67 | + |
| 68 | + if (Array.isArray(obj)) { |
| 69 | + return obj.map(redactSensitiveData); |
| 70 | + } |
| 71 | + |
| 72 | + if (typeof obj === 'object') { |
| 73 | + const redacted: any = {}; |
| 74 | + for (const [key, value] of Object.entries(obj)) { |
| 75 | + // Special handling for card objects - only redact cvv and number |
| 76 | + if (key.toLowerCase() === 'card' && typeof value === 'object' && value !== null) { |
| 77 | + redacted[key] = redactCardObject(value); |
| 78 | + } |
| 79 | + // Check if key matches sensitive patterns |
| 80 | + else if (SENSITIVE_PATTERNS.some(pattern => pattern.test(key))) { |
| 81 | + redacted[key] = '[REDACTED]'; |
| 82 | + } else { |
| 83 | + redacted[key] = redactSensitiveData(value); |
| 84 | + } |
| 85 | + } |
| 86 | + return redacted; |
| 87 | + } |
| 88 | + |
| 89 | + return obj; |
| 90 | +} |
| 91 | + |
| 92 | +class Logger { |
| 93 | + private currentLogLevel: LogLevel; |
| 94 | + |
| 95 | + constructor() { |
| 96 | + this.currentLogLevel = config.LOG_LEVEL as LogLevel; |
| 97 | + } |
| 98 | + |
| 99 | + private shouldLog(level: LogLevel): boolean { |
| 100 | + return logLevelValues[level] >= logLevelValues[this.currentLogLevel]; |
| 101 | + } |
| 102 | + |
| 103 | + private formatLog(level: LogLevel, message: string, meta?: any) { |
| 104 | + const timestamp = new Date().toISOString(); |
| 105 | + const logEntry = { |
| 106 | + timestamp, |
| 107 | + level, |
| 108 | + message, |
| 109 | + ...(meta && { meta: redactSensitiveData(meta) }), |
| 110 | + }; |
| 111 | + |
| 112 | + return JSON.stringify(logEntry); |
| 113 | + } |
| 114 | + |
| 115 | + debug(message: string, meta?: any) { |
| 116 | + // Disabled for MCP stdio communication |
| 117 | + if (this.shouldLog(LogLevel.DEBUG)) { |
| 118 | + console.error(this.formatLog(LogLevel.DEBUG, message, meta)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + info(message: string, meta?: any) { |
| 123 | + // Disabled for MCP stdio communication |
| 124 | + if (this.shouldLog(LogLevel.INFO)) { |
| 125 | + console.error(this.formatLog(LogLevel.INFO, message, meta)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + warn(message: string, meta?: any) { |
| 130 | + // Disabled for MCP stdio communication |
| 131 | + if (this.shouldLog(LogLevel.WARN)) { |
| 132 | + console.error(this.formatLog(LogLevel.WARN, message, meta)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + error(message: string, meta?: any) { |
| 137 | + // Disabled for MCP stdio communication |
| 138 | + if (this.shouldLog(LogLevel.ERROR)) { |
| 139 | + console.error(this.formatLog(LogLevel.ERROR, message, meta)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Log API request |
| 145 | + */ |
| 146 | + logRequest(method: string, url: string, data?: any, headers?: any) { |
| 147 | + this.debug('API Request', { |
| 148 | + method, |
| 149 | + url, |
| 150 | + data, |
| 151 | + headers, |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Log API response |
| 157 | + */ |
| 158 | + logResponse(method: string, url: string, status: number, data?: any) { |
| 159 | + this.debug('API Response', { |
| 160 | + method, |
| 161 | + url, |
| 162 | + status, |
| 163 | + data, |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Log tool call |
| 169 | + */ |
| 170 | + logToolCall(toolName: string, params?: any) { |
| 171 | + this.info('Tool called', { |
| 172 | + tool: toolName, |
| 173 | + params, |
| 174 | + }); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +export const logger = new Logger(); |
0 commit comments