All files / src/modules/log DefaultLogger.ts

97.67% Statements 42/43
90% Branches 9/10
100% Functions 14/14
97.67% Lines 42/43

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      1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                         11x   11x   11x     1x       1x       1x       1108x 1094x   14x         1037x       14x       13x       12x       25x       7x       2211x 2210x   1x   1x 1x               2211x          
import CoreLogger from '../../classes/CoreLogger';
import { XUtil } from '../../utils/XUtil';
 
export enum DC {
  'Reset' = '\x1b[0m',
  'Bright' = '\x1b[1m',
  'Dim' = '\x1b[2m',
  'Underscore' = '\x1b[4m',
  'FgBlack' = '\x1b[30m',
  'FgRed' = '\x1b[31m',
  'FgGreen' = '\x1b[32m',
  'FgYellow' = '\x1b[33m',
  'FgBlue' = '\x1b[34m',
  'FgMagenta' = '\x1b[35m',
  'FgCyan' = '\x1b[36m',
  'FgWhite' = '\x1b[37m',
  'BgBlack' = '\x1b[40m',
  'BgRed' = '\x1b[41m',
  'BgGreen' = '\x1b[42m',
  'BgYellow' = '\x1b[43m',
  'BgBlue' = '\x1b[44m',
  'BgMagenta' = '\x1b[45m',
  'BgCyan' = '\x1b[46m',
  'BgWhite' = '\x1b[47m',
}
export type DefaultLogMode = 'D' | 'E' | 'I' | 'L' | 'V' | 'W';
 
/**
 * Logger implementation that outputs messages to the console with optional color,
 * timestamps, and object stringification. The logger categorizes messages by level
 * (debug, error, info, log, verbose, warn) and prefixes each line with a mode
 * identifier and an optional timestamp.
 *
 * @extends CoreLogger
 */
export class DefaultLogger extends CoreLogger {
  private noColor = false;
 
  private printObject = false;
 
  private printTimeStamp = true;
 
  setNoColor(val: boolean) {
    this.noColor = val;
  }
 
  setPrintTimestamp(val: boolean) {
    this.printTimeStamp = val;
  }
 
  setPrintObject(val: boolean) {
    this.printObject = val;
  }
 
  logColor(args: DC[], msg: string) {
    if (args.length > 0 && !this.noColor) {
      console.log([...args, msg, DC.Reset].join(''));
    } else {
      console.log(msg);
    }
  }
 
  debug(channel: string, ...ags: unknown[]): void {
    this.logColor([DC.FgMagenta], this.format('D', channel, ...ags));
  }
 
  error(channel: string, ...ags: unknown[]): void {
    this.logColor([DC.FgRed], this.format('E', channel, ...ags));
  }
 
  info(channel: string, ...ags: unknown[]): void {
    this.logColor([DC.FgGreen], this.format('I', channel, ...ags));
  }
 
  log(channel: string, ...ags: unknown[]): void {
    this.logColor([], this.format('L', channel, ...ags));
  }
 
  verbose(channel: string, ...ags: unknown[]): void {
    this.logColor([DC.FgBlue], this.format('V', channel, ...ags));
  }
 
  warn(channel: string, ...ags: unknown[]): void {
    this.logColor([DC.BgYellow, DC.FgBlack], this.format('W', channel, ...ags));
  }
 
  private printArgs(...args: unknown[]) {
    if (!this.printObject) {
      return args;
    }
    return args
      .map((arg) => {
        Eif (typeof arg === 'object') {
          return JSON.stringify(arg);
        }
        return arg;
      })
      .join(',');
  }
 
  format(mode: DefaultLogMode, channel: string, ...args: unknown[]): string {
    return `[${mode}]${
      this.printTimeStamp ? `[${XUtil.getTimeStamp()}]` : ''
    }(${channel}) ${this.printArgs(args)}`;
  }
}