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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                              53x                               53x             6x 6x                                             81x 14x   67x   81x                     3x 1x   2x   3x                     3x 2x   1x   3x                     28x 27x   1x   28x          
/**
 * 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.
 *
 * @example
 * ```typescript
 * import { logger } from './utils/logger';
 *
 * // Basic logging
 * logger.info('Application started');
 * 
 * // Logging with context
 * logger.warn('High memory usage', { memory: '85%', threshold: '80%' });
 * 
 * // Error logging
 * try {
 *   processData();
 * } catch (error) {
 *   logger.error('Data processing failed', error);
 * }
 * 
 * // Debug logging (development only)
 * logger.debug('Security calculation', { confidentiality: 'High', integrity: 'Moderate' });
 * ```
 */
 
// 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: unknown[]): Logger;
  info(message: string, context?: unknown): Logger;
  warn(message: string, context?: unknown): Logger;
  error(message: string, context?: unknown): Logger;
  debug(message: string, context?: unknown): 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: unknown[]): 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
   * 
   * @example
   * ```typescript
   * // Simple debug message
   * logger.debug('Rendering widget');
   * 
   * // Debug with context
   * logger.debug('Service call', { 
   *   endpoint: '/api/security', 
   *   params: { level: 'High' } 
   * });
   * ```
   */
  debug(message: string, context?: unknown): 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?: unknown): 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?: unknown): 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?: unknown): typeof logger {
    if (context !== undefined) {
      console.error(PREFIX, message, context);
    } else {
      console.error(PREFIX, message);
    }
    return logger;
  },
};
 
export default logger;