All files / app/services/log/logEntry logEntry.ts

100% Statements 28/28
100% Branches 32/32
100% Functions 5/5
100% Lines 26/26

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                  4988x   4988x   4988x   4988x   4988x         4988x                   4988x                 674x   674x 30x       674x         674x         674x         674x 602x             674x                   602x             1224x 44x     44x 132x               44x 25x       602x                       226x 220x      
import { IEnvironment } from 'src/environments/interface/ienvironment';
import { LogLevel } from '../logLevel/logLevel';
 
/**
 * Represents a log entry, with all the information to log and provides methods
 * to format the log message appropriately.
 */
export class LogEntry {
  /** The date of creation of the entry */
  entryDate: Date = new Date();
  /** The message */
  message = '';
  /** The {@link LogLevel} of the entry */
  level: LogLevel = LogLevel.Debug;
  /** Optional objects */
  extraInfo: unknown[] = [];
  /** If the log entry should take the date into account */
  logWithDate = true;
  /**
   * The class name used in the log, used to identify where the log comes from
   * in the code, and should be specified when creating a logger.
   */
  className = '';
  /** If the logs should be compact */
  compactDisplay: boolean;
 
  /**
   * LogEntry constructor
   *
   * @param environment The environment
   */
  constructor(environment: IEnvironment) {
    this.compactDisplay = environment.compactLogDisplay;
  }
 
  /**
   * Builds the string to log using all of the information given.
   *
   * @returns The string to log
   */
  buildLogString(): string {
    let ret = '';
 
    if (this.logWithDate) {
      ret =
        this.entryDate.toLocaleString() + (this.compactDisplay ? ' - ' : '');
    }
 
    ret +=
      (this.compactDisplay ? '' : '\n\t') +
      'Class:' +
      (this.compactDisplay ? ' ' : '\t') +
      this.className;
    ret +=
      (this.compactDisplay ? ' - ' : '\n\t') +
      'Type:' +
      (this.compactDisplay ? ' ' : '\t') +
      LogLevel[this.level];
    ret +=
      (this.compactDisplay ? ' - ' : '\n\t\t') +
      'Message:' +
      (this.compactDisplay ? ' ' : '\t') +
      this.message;
    if (this.extraInfo.length) {
      ret +=
        (this.compactDisplay ? ' - ' : '\n\t\t') +
        'Extra Info:' +
        (this.compactDisplay ? ' ' : '\t') +
        this.formatParams(this.extraInfo);
    }
 
    return ret;
  }
 
  /**
   * Format the optional objects.
   *
   * @param params The optional objects
   * @returns A string, displaying them properly
   */
  private formatParams(params: unknown[]): string {
    let ret: string = params.join(
      this.compactDisplay
        ? ','
        : '\n--------------------------------------------------------------------------------\n\t\t\t\t\t'
    );
 
    // Is there at least one object in the array?
    if (params.some((p) => typeof p == 'object')) {
      ret = '';
 
      // Build comma-delimited string
      for (const item of params) {
        ret +=
          JSON.stringify(item, this.replacer, this.compactDisplay ? '' : '\t\t')
            .split('\n')
            .join(this.compactDisplay ? '' : '\n\t\t\t\t\t') +
          (this.compactDisplay
            ? ','
            : '\n--------------------------------------------------------------------------------\n\t\t\t\t\t');
      }
      if (ret.endsWith(',')) {
        ret = ret.slice(0, -1);
      }
    }
 
    return ret;
  }
 
  /**
   * Replacer method used to convert to JSON and remove information from the
   * logs.
   *
   * @param key The key
   * @param value The value
   * @returns The replaced value
   */
  private replacer(key: string, value: unknown) {
    if (key == 'secret') return undefined;
    else return value;
  }
}