All files / logger level-utils.ts

92% Statements 23/25
95.45% Branches 21/22
100% Functions 3/3
90.9% Lines 20/22

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 12528x                 28x 51x 51x                   18x     33x 33x                                                                     28x 2731x   268x     1524x     537x     39x     115x     131x     40x     38x     39x                                                                     28x 1374x  
import { exhaustiveCheck } from '../utils/common/exhaustive-check.ts';
import type { LogLevel } from './definition.ts';
 
/**
 * Checks if a string is a valid LogLevel.
 *
 * @param value The string to check
 * @returns True if the string is a valid LogLevel, false otherwise
 */
export const isLogLevel = (value: unknown): value is LogLevel => {
  const level = value as LogLevel;
  switch (level) {
    case 'trace':
    case 'debug':
    case 'info':
    case 'notice':
    case 'warning':
    case 'error':
    case 'critical':
    case 'alert':
    case 'emergency':
      return true;
 
    default:
      exhaustiveCheck(level);
      return false;
  }
};
 
/**
 * Converts a LogLevel to its corresponding numeric weight value for comparison operations.
 *
 * This function maps each log level to a numeric value according to its severity, with lower numbers representing
 * higher severity (more important) levels:
 *
 * - Emergency: 0 (highest severity)
 * - Alert: 1
 * - Critical: 2
 * - Error: 3
 * - Warning: 4
 * - Notice: 5
 * - Info: 6
 * - Debug: 7
 * - Trace: 8 (lowest severity)
 *
 * @example
 *
 * ```ts
 * const errorWeight = toLevelWeight('error'); // Returns 3
 * const debugWeight = toLevelWeight('debug'); // Returns 7
 *
 * // Compare severity levels
 * if (toLevelWeight('error') < toLevelWeight('debug')) {
 *   // This condition is true (3 < 7), meaning 'error' is more severe than 'debug'
 * }
 * ```
 *
 * @param level The log level to convert to a numeric weight
 * @returns The numeric weight corresponding to the specified log level
 */
export const toLevelWeight = (level: LogLevel): number => {
  switch (level) {
    case 'trace':
      return 8;
 
    case 'debug':
      return 7;
 
    case 'info':
      return 6;
 
    case 'notice':
      return 5;
 
    case 'warning':
      return 4;
 
    case 'error':
      return 3;
 
    case 'critical':
      return 2;
 
    case 'alert':
      return 1;
 
    case 'emergency':
      return 0;
 
    default:
      exhaustiveCheck(level);
      return 20;
  }
};
 
/**
 * Determines whether a log entry should be emitted based on the configured logger level and the entry's level.
 *
 * This function implements the severity filtering logic of the logger:
 *
 * - When the logger level is 'off', no entries will be emitted
 * - Otherwise, entries are emitted when their level's severity is equal to or greater than the logger's level
 *
 * For example, if the logger's level is set to 'warning', entries with levels 'warning', 'error', 'critical', 'alert',
 * and 'emergency' will be emitted, while entries with levels 'notice', 'info', 'debug', and 'trace' will be filtered
 * out.
 *
 * @example
 *
 * ```ts
 * // When logger level is 'warning'
 * shouldEmitEntry('warning', 'error'); // Returns true (error entries are emitted)
 * shouldEmitEntry('warning', 'info'); // Returns false (info entries are filtered out)
 *
 * // When logger is turned off
 * shouldEmitEntry('off', 'emergency'); // Returns false (nothing is emitted)
 * ```
 *
 * @param level The current configured level of the logger or 'off'
 * @param entryLevel The severity level of the log entry to be evaluated
 * @returns True if the entry should be emitted, false otherwise
 */
export const shouldEmitEntry = (level: LogLevel | 'off', entryLevel: LogLevel): boolean =>
  level !== 'off' && toLevelWeight(entryLevel) <= toLevelWeight(level);