UNPKG

webrtc2-logger

Version:

WebRTC2 Logger - Structured logging system for WebRTC applications with cross-platform support, debugging tools, and performance monitoring

100 lines (82 loc) 2.63 kB
/** * @webrtc2/logger * * Structured logging for WebRTC2 ecosystem */ export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; export interface LogEntry { level: LogLevel; message: string; timestamp: Date; context?: Record<string, any>; component?: string; } export class Logger { private component: string; private minLevel: LogLevel; private levels: Record<LogLevel, number> = { debug: 0, info: 1, warn: 2, error: 3 }; constructor(component: string, minLevel: LogLevel = 'info') { this.component = component; this.minLevel = minLevel; } private shouldLog(level: LogLevel): boolean { return this.levels[level] >= this.levels[this.minLevel]; } private log(level: LogLevel, message: string, context?: Record<string, any>): void { if (!this.shouldLog(level)) return; const entry: LogEntry = { level, message, timestamp: new Date(), context, component: this.component }; // Format output const timestamp = entry.timestamp.toISOString(); const prefix = `[${timestamp}] [${level.toUpperCase()}] [${this.component}]`; const contextStr = context ? ` ${JSON.stringify(context)}` : ''; const output = `${prefix} ${message}${contextStr}`; // Output to appropriate console method switch (level) { case 'debug': console.debug(output); break; case 'info': console.info(output); break; case 'warn': console.warn(output); break; case 'error': console.error(output); break; } } debug(message: string, context?: Record<string, any>): void { this.log('debug', message, context); } info(message: string, context?: Record<string, any>): void { this.log('info', message, context); } warn(message: string, context?: Record<string, any>): void { this.log('warn', message, context); } error(message: string, context?: Record<string, any>): void { this.log('error', message, context); } setLevel(level: LogLevel): void { this.minLevel = level; } } // Default logger instance export const logger = new Logger('webrtc2'); // Create component-specific loggers export function createLogger(component: string, level?: LogLevel): Logger { return new Logger(component, level); } export const VERSION = '1.0.0';