/ profilers / logger.js
logger.js
 1  /**
 2   * @fileoverview Centralized logging utility for profilers and tests.
 3   *
 4   * This module provides logging functions that respect a global debug flag,
 5   * allowing for conditional logging based on the environment.
 6   */
 7  
 8  /**
 9   * Determines if debug logging is enabled
10   * In production, this will be false
11   * In development, this will be true
12   *
13   * Uses hostname to determine environment since process.env is not available in browsers
14   */
15  export const DEBUG_MODE = (() => {
16    // Check if we're in a development environment based on hostname
17    const hostname = window.location.hostname;
18    return hostname === 'localhost' ||
19           hostname === '127.0.0.1' ||
20           hostname.includes('loophole.site') ||
21           hostname.endsWith('.local') ||
22           hostname.includes('dev') ||
23           hostname.includes('test');
24  })();
25  
26  /**
27   * Log levels
28   */
29  export const LogLevel = {
30    DEBUG: 'debug',
31    INFO: 'info',
32    WARN: 'warn',
33    ERROR: 'error',
34  };
35  
36  /**
37   * Log a debug message (only in debug mode)
38   * @param {...any} args - Arguments to log
39   */
40  export function debug(...args) {
41    if (DEBUG_MODE) {
42      console.debug(...args);
43    }
44  }
45  
46  /**
47   * Log an info message
48   * @param {...any} args - Arguments to log
49   */
50  export function info(...args) {
51    console.info(...args);
52  }
53  
54  /**
55   * Log a warning message
56   * @param {...any} args - Arguments to log
57   */
58  export function warn(...args) {
59    console.warn(...args);
60  }
61  
62  /**
63   * Log an error message
64   * @param {...any} args - Arguments to log
65   */
66  export function error(...args) {
67    console.error(...args);
68  }
69  
70  /**
71   * Create a logger with a specific prefix
72   * @param {string} prefix - Prefix to add to all log messages
73   * @returns {Object} Logger object with debug, info, warn, and error methods
74   */
75  export function createLogger(prefix) {
76    return {
77      debug: (...args) => debug(`[${prefix}]`, ...args),
78      info: (...args) => info(`[${prefix}]`, ...args),
79      warn: (...args) => warn(`[${prefix}]`, ...args),
80      error: (...args) => error(`[${prefix}]`, ...args),
81    };
82  }