All files / src/utils logger.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 5/5
100% Lines 20/20

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 101 102 103 104                    35x                               35x             6x 6x                     2x 1x   1x   2x                     3x 1x   2x   3x                     3x 2x   1x   3x                     3x 2x   1x   3x          
/**
 * Simple logger utility for consistent logging throughout the application
 *
 * ## Technical Perspective
 *
 * Provides standardized logging across the application with different log levels.
 * Can be extended to support more advanced features like remote logging or log rotation.
 */
 
// Define a prefix for all log messages
const PREFIX = "[CIA-CM]";
 
/**
 * Logger interface to define the shape of our logger object
 */
interface Logger {
  log(...args: any[]): Logger;
  info(message: string, context?: any): Logger;
  warn(message: string, context?: any): Logger;
  error(message: string, context?: any): Logger;
  debug(message: string, context?: any): Logger;
}
 
/**
 * Simple logger interface with different log levels
 */
const logger: Logger = {
  /**
   * Log a message (same as console.log)
   * @param args - Arguments to log
   * @returns The logger instance for chaining
   */
  log(...args: any[]): typeof logger {
    console.log(PREFIX, ...args);
    return logger;
  },
 
  /**
   * Log debug message
   *
   * @param message - Message to log
   * @param context - Optional context object
   * @returns The logger instance for chaining
   */
  debug(message: string, context?: any): typeof logger {
    if (context !== undefined) {
      console.debug(PREFIX, message, context);
    } else {
      console.debug(PREFIX, message);
    }
    return logger;
  },
 
  /**
   * Log info message
   *
   * @param message - Message to log
   * @param context - Optional context object
   * @returns The logger instance for chaining
   */
  info(message: string, context?: any): typeof logger {
    if (context !== undefined) {
      console.info(PREFIX, message, context);
    } else {
      console.info(PREFIX, message);
    }
    return logger;
  },
 
  /**
   * Log warning message
   *
   * @param message - Message to log
   * @param context - Optional context object
   * @returns The logger instance for chaining
   */
  warn(message: string, context?: any): typeof logger {
    if (context !== undefined) {
      console.warn(PREFIX, message, context);
    } else {
      console.warn(PREFIX, message);
    }
    return logger;
  },
 
  /**
   * Log error message
   *
   * @param message - Message to log
   * @param context - Optional context object
   * @returns The logger instance for chaining
   */
  error(message: string, context?: any): typeof logger {
    if (context !== undefined) {
      console.error(PREFIX, message, context);
    } else {
      console.error(PREFIX, message);
    }
    return logger;
  },
};
 
export default logger;