All files / common/util debug-logger.ts

100% Statements 59/59
100% Branches 24/24
100% Functions 13/13
100% Lines 54/54

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 138 139 140 141 142 143 14410x   10x                                         10x 10x 10x 10x 10x     10x                     10x 16x 16x 16x 16x 16x           10x 10x   10x   8x     883x       10x           10x 883x           10x 15x 5x 3x       2x     36x       36x     2x 2x     2x     10x 1x 1x     9x 1x 1x     8x 8x   8x 8x       8x   8x 8x   3x 3x   1x 1x   1x 1x   3x 3x         10x        
import {STRINGS} from '../../if-run/config';
 
const logMessagesKeys: (keyof typeof STRINGS)[] = [
  'STARTING_IF',
  'EXITING_IF',
  'LOADING_MANIFEST',
  'VALIDATING_MANIFEST',
  'CAPTURING_RUNTIME_ENVIRONMENT_DATA',
  'CHECKING_AGGREGATION_METHOD',
  'INITIALIZING_PLUGINS',
  'INITIALIZING_PLUGIN',
  'LOADING_PLUGIN_FROM_PATH',
  'COMPUTING_PIPELINE_FOR_NODE',
  'COMPUTING_COMPONENT_PIPELINE',
  'REGROUPING',
  'OBSERVING',
  'MERGING_DEFAULTS_WITH_INPUT_DATA',
  'AGGREGATING_OUTPUTS',
  'AGGREGATING_NODE',
  'PREPARING_OUTPUT_DATA',
  'EXPORTING_TO_YAML_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 =
      typeof args[0] === 'string' &&
      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 (typeof args[0] === 'string' && args[0].includes('# start')) {
    originalConsole.log(...args);
    return;
  }
 
  if (args[0] === '\n') {
    originalConsole.log();
    return;
  }
 
  const date = new Date().toISOString();
  const plugin = pluginNameManager.currentPluginName;
  const isExeption =
    typeof args[0] === 'string' && args[0].includes('**Computing');
  const message = `${level}: ${date}: ${plugin ? plugin + ': ' : ''}${args.join(
    ', '
  )}`;
 
  const formattedMessage = isExeption ? args.join(', ') : message;
 
  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,
};