UNPKG

@200systems/mf-logger

Version:

Structured logging with multiple outputs and performance timing for TypeScript applications

46 lines 1.84 kB
export class LogFormatter { colors = { debug: '\x1b[36m', // cyan info: '\x1b[32m', // green warn: '\x1b[33m', // yellow error: '\x1b[31m', // red reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m', }; formatJson(entry) { return JSON.stringify(entry); } formatPretty(entry) { const timestamp = this.formatTimestamp(entry.timestamp); const level = this.formatLevel(entry.level); const context = entry.context ? `[${entry.context}]` : ''; const message = entry.message; let output = `${timestamp} ${level} ${context} ${message}`.trim(); if (entry.metadata && Object.keys(entry.metadata).length > 0) { output += `\n${this.colors.dim}${JSON.stringify(entry.metadata, null, 2)}${this.colors.reset}`; } if (entry.error?.stack) { output += `\n${this.colors.dim}${entry.error.stack}${this.colors.reset}`; } if (entry.request) { const req = entry.request; const reqInfo = `${req.method} ${req.url} ${req.ip || ''} ${req.userAgent || ''}`.trim(); output += `\n${this.colors.dim}Request: ${reqInfo}${this.colors.reset}`; } if (entry.performance?.duration) { output += `\n${this.colors.dim}Duration: ${entry.performance.duration}ms${this.colors.reset}`; } return output; } formatTimestamp(timestamp) { return `${this.colors.dim}${timestamp}${this.colors.reset}`; } formatLevel(level) { const color = this.colors[level]; const levelStr = level.toUpperCase().padEnd(5); return `${color}${this.colors.bold}${levelStr}${this.colors.reset}`; } } export const defaultFormatter = new LogFormatter(); //# sourceMappingURL=formatter.js.map