All files / shared/utils/src logger.ts

0% Statements 0/29
0% Branches 0/21
0% Functions 0/11
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
/**
 * Centralized logger utility for the BuzzTrail extension
 * Replaces console statements with environment-aware logging
 */
 
export enum LogLevel {
  DEBUG = 0,
  INFO = 1,
  WARN = 2,
  ERROR = 3
}
 
export interface LogContext {
  module?: string;
  function?: string;
  userId?: string;
  sessionId?: string;
  [key: string]: unknown;
}
 
class Logger {
  private level: LogLevel;
  private isDevelopment: boolean;
 
  constructor() {
    this.isDevelopment = process.env.NODE_ENV === 'development' || 
                        process.env.NODE_ENV === 'test' ||
                        typeof window !== 'undefined';
    this.level = this.isDevelopment ? LogLevel.DEBUG : LogLevel.INFO;
  }
 
  private formatMessage(level: string, message: string, context?: LogContext): string {
    const timestamp = new Date().toISOString();
    const contextStr = context ? ` [${JSON.stringify(context)}]` : '';
    return `[${timestamp}] ${level}: ${message}${contextStr}`;
  }
 
  private shouldLog(level: LogLevel): boolean {
    return level >= this.level;
  }
 
  debug(message: string, ...args: unknown[]): void {
    Iif (this.shouldLog(LogLevel.DEBUG) && this.isDevelopment) {
      console.log(this.formatMessage('DEBUG', message), ...args);
    }
  }
 
  info(message: string, ...args: unknown[]): void {
    Iif (this.shouldLog(LogLevel.INFO)) {
      console.info(this.formatMessage('INFO', message), ...args);
    }
  }
 
  warn(message: string, ...args: unknown[]): void {
    Iif (this.shouldLog(LogLevel.WARN)) {
      console.warn(this.formatMessage('WARN', message), ...args);
    }
  }
 
  error(message: string, error?: Error, ...args: unknown[]): void {
    Iif (this.shouldLog(LogLevel.ERROR)) {
      const errorDetails = error ? `\nError: ${error.message}\nStack: ${error.stack}` : '';
      console.error(this.formatMessage('ERROR', message + errorDetails), ...args);
    }
  }
 
  // Specialized logging for API calls
  apiCall(method: string, url: string, status?: number, duration?: number): void {
    const message = `API ${method} ${url}`;
    const context: LogContext = {
      module: 'api',
      function: 'apiCall',
      status,
      duration
    };
    
    if (status && status >= 400) {
      this.error(message, undefined, context);
    } else {
      this.info(message, context);
    }
  }
 
  // Specialized logging for user interactions
  userAction(action: string, details?: unknown): void {
    this.info(`User Action: ${action}`, { module: 'user', action, details });
  }
 
  // Specialized logging for system events
  systemEvent(event: string, details?: unknown): void {
    this.info(`System Event: ${event}`, { module: 'system', event, details });
  }
}
 
// Export singleton instance
export const logger = new Logger();
 
// Export individual methods for convenience
export const { debug, info, warn, error, apiCall, userAction, systemEvent } = logger;