All files / util debug-logger.ts

100% Statements 54/54
100% Branches 15/15
100% Functions 13/13
100% Lines 49/49

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 1339x   9x                                         9x 9x 9x 9x 9x     9x                     9x 13x 13x 13x 13x 13x           9x 9x   9x   6x     1171x       9x           9x 1171x           9x 10x 3x 1x     2x   36x       36x     2x 2x     2x     7x 1x 1x     6x 6x 6x       6x 6x   3x 3x   1x 1x   1x 1x   1x 1x         9x        
import {STRINGS} from '../config/strings';
 
const logMessagesKeys: (keyof typeof STRINGS)[] = [
  'STARTING_IF',
  'EXITING_IF',
  'LOADING_MANIFEST',
  'VALIDATING_MANIFEST',
  'CAPTURING_RUNTIME_ENVIRONMENT_DATA',
  'SYNCING_PARAMETERS',
  'CHECKING_AGGREGATION_METHOD',
  'INITIALIZING_PLUGINS',
  'INITIALIZING_PLUGIN',
  'LOADING_PLUGIN_FROM_PATH',
  'COMPUTING_PIPELINE_FOR_NODE',
  'MERGING_DEFAULTS_WITH_INPUT_DATA',
  'AGGREGATING_OUTPUTS',
  'AGGREGATING_NODE',
  'PREPARING_OUTPUT_DATA',
  'EXPORTING_TO_YAML_FILE',
  'EXPORTING_TO_CSV_FILE',
  'EXPORTING_RAW_CSV_FILE',
];
 
enum LogLevel {
  Info = 'INFO',
  Warn = 'WARN',
  Error = 'ERROR',
  Debug = 'DEBUG',
}
 
const originalConsole = {
  log: console.log,
  info: console.info,
  warn: console.warn,
  error: console.error,
  debug: console.debug,
};
 
/**
 * Overrides console methods with custom debug logging.
 */
const overrideConsoleMethods = (debugMode: boolean) => {
  console.log = (...args: any[]) => debugLog(LogLevel.Info, args, debugMode);
  console.info = (...args: any[]) => debugLog(LogLevel.Info, args, debugMode);
  console.warn = (...args: any[]) => debugLog(LogLevel.Warn, args, debugMode);
  console.error = (...args: any[]) => debugLog(LogLevel.Error, args, debugMode);
  console.debug = (...args: any[]) => debugLog(LogLevel.Debug, args, debugMode);
};
 
/**
 * Creates an encapsulated object to retrieve the plugin name.
 */
const pluginNameManager = (() => {
  let pluginName: string | undefined = '';
 
  const manager = {
    get currentPluginName() {
      return pluginName;
    },
    set currentPluginName(value: string | undefined) {
      pluginName = value;
    },
  };
 
  return manager;
})();
 
/**
 * Sets the name of the currently executing plugin.
 */
const setExecutingPluginName = (pluginName?: string) => {
  pluginNameManager.currentPluginName = pluginName;
};
 
/**
 * Logs messages with the specified log level and format.
 */
const debugLog = (level: LogLevel, args: any[], debugMode: boolean) => {
  if (!debugMode) {
    if (level === LogLevel.Debug) {
      return;
    }
 
    const isDebugLog = logMessagesKeys.some(key => {
      const message =
        typeof STRINGS[key] === 'function'
          ? (STRINGS[key] as Function).call(null, '')
          : (STRINGS[key] as string);
 
      return args[0].includes(message);
    });
 
    if (!isDebugLog) {
      originalConsole.log(...args);
    }
 
    return;
  }
 
  if (args[0].includes('# start')) {
    originalConsole.log(...args);
    return;
  }
 
  const date = new Date().toISOString();
  const plugin = pluginNameManager.currentPluginName;
  const formattedMessage = `${level}: ${date}: ${
    plugin ? plugin + ': ' : ''
  }${args.join(', ')}`;
 
  if (debugMode) {
    switch (level) {
      case LogLevel.Info:
        originalConsole.info(formattedMessage);
        break;
      case LogLevel.Warn:
        originalConsole.warn(formattedMessage);
        break;
      case LogLevel.Error:
        originalConsole.error(formattedMessage);
        break;
      case LogLevel.Debug:
        originalConsole.debug(formattedMessage);
        break;
    }
  }
};
 
export const debugLogger = {
  overrideConsoleMethods,
  setExecutingPluginName,
};