Code coverage report for core/src/log/ConsoleLog.ts

Statements: 54% (27 / 50)      Branches: 49.35% (38 / 77)      Functions: 71.43% (5 / 7)      Lines: 54% (27 / 50)      Ignored: none     

All files » core/src/log/ » ConsoleLog.ts
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 144 145 146 147 148 149 150 151 152 153 154 155 156 1571 1 1 1 1                                                                         1         46 46             2         22           22     22       22   22                     2 2                 2       2   2 2       2               1 1               1 1                     2 2                                        
import {LOG_ENV} from './constants';
import {Log, LogOptions} from './log';
import {LogConfig} from './log-config';
import {LogLevel} from './LogLevel';
import {Message, MessageContext} from './message';
 
 
export type ConsoleApi = {
                           memory?: { totalJSHeapSize: number; usedJSHeapSize: number; jsHeapSizeLimit: number };
                           context?: unknown;
                           assert?: (value: any, message?: string, ...optionalParams: any[]) => void;
                           clear?: () => void;
                           dir?: (obj: any, optional?: any) => void;
                           dirxml?: (...data: any[]) => void;
 
                           table: (tabularData: any, properties?: string[]) => void;
                           count: (label?: string) => void;
                           countReset: (label?: string) => void;
                           time: (label?: string) => void;
                           timeEnd: (label?: string) => void;
                           timeLog: (label?: string) => void;
                           profile: (label?: string) => void;
                           profileEnd: (label?: string) => void;
                           timeStamp: (label?: string) => void;
                           group: (...label: any[]) => void;
                           groupCollapsed: (...label: any[]) => void;
                           groupEnd: () => void;
                           trace: (message?: any, ...optionalParams: any[]) => void;
                           debug: (message?: any, ...optionalParams: any[]) => void;
                           warn: (message?: any, ...optionalParams: any[]) => void;
                           error: (message?: any, ...optionalParams: any[]) => void;
                           info: (message?: any, ...optionalParams: any[]) => void;
                           log: (message?: any, ...optionalParams: any[]) => void;
                         } | Console;
 
export type ConsoleLogOptions = LogOptions & {
  console?: ConsoleApi | Console;
  parent?: ConsoleLog;
}
 
 
export class ConsoleLog extends Log {
  readonly console: ConsoleApi;
 
 
  protected constructor(name: string, config: LogConfig, parent?: ConsoleLog, _console?: ConsoleApi) {
    super(name, config, parent as Log);
    this.console = _console || (
      parent && parent instanceof ConsoleLog ? parent.console : console
    );
  }
 
 
  extend(name: string, config?: LogConfig): ConsoleLog {
    return new ConsoleLog(name, config || this.config, this, this.console);
  }
 
 
  copy(name: string, config?: LogConfig): ConsoleLog {
    return new ConsoleLog(name, config || this.config, undefined, this.console);
  }
 
 
  static create(options?: ConsoleLogOptions): ConsoleLog {
    let config: LogConfig;
    Iif (options?.config && options.parent) {
      config = options.parent.config.extend(LogConfig.create(options.config));
    }
    else Iif (options?.parent) {
      config = options.parent.config;
    }
    else {
      config = LogConfig.create(options?.config);
    }
    return new ConsoleLog(
      options?.name || '',
      config,
      options?.parent,
      options?.console,
    );
  }
 
 
  protected _write(level: LogLevel, messageContext: MessageContext, ...args: unknown[]): void | Error {
    let formatted: string;
    try {
      const context = new MessageContext(messageContext.message, {
        ...this.config.contextSupplier(),
        name: this.name,
        fqn: this.fqn,
        level,
        ...messageContext.data,
        args,
      });
 
      Iif (this.config.messageFormat) {
        formatted = this.config.messageTemplate.format(context);
      }
      else {
        formatted = context.message;
      }
      let includeArgs = true;
      Iif (this.config.messageFormat) {
        formatted = this.config.messageFormat(formatted, ...args);
        includeArgs = false;
      }
      switch (level) {
        case LogLevel.EMERGENCY:
          includeArgs ? this.console.error(formatted, ...args) : this.console.error(formatted);
          break;
        case LogLevel.ALERT:
          includeArgs ? this.console.error(formatted, ...args) : this.console.error(formatted);
          break;
        case LogLevel.CRITICAL:
          includeArgs ? this.console.error(formatted, ...args) : this.console.error(formatted);
          break;
        case LogLevel.ERROR:
          includeArgs ? this.console.error(formatted, ...args) : this.console.error(formatted);
          break;
        case LogLevel.WARN:
          includeArgs ? this.console.warn(formatted, ...args) : this.console.warn(formatted);
          break;
        case LogLevel.INFO:
          includeArgs ? this.console.info(formatted, ...args) : this.console.info(formatted);
          break;
        case LogLevel.DEBUG:
          includeArgs ? this.console.debug(formatted, ...args) : this.console.debug(formatted);
          break;
        case LogLevel.TRACE:
          includeArgs ? this.console.trace(formatted, ...args) : this.console.trace(formatted);
          break;
        default:
          includeArgs ? this.console.log(formatted, ...args) : this.console.log(formatted);
 
      }
      Eif (this.config.interceptors) {
        this.config.interceptors.forEach(interceptor => {
          interceptor(new Message(level, context));
        });
      }
 
    }
    catch (error) {
      console.error('Error writing message %m');
      return error;
    }
  }
 
 
  table(tabularData: any, properties: string[]): void {
    if (this.enabled(LOG_ENV.table)) {
      this.console.table(tabularData, properties);
    }
  }
 
}