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

Statements: 70.27% (26 / 37)      Branches: 65.22% (15 / 23)      Functions: 85.71% (6 / 7)      Lines: 69.44% (25 / 36)      Ignored: none     

All files » core/src/internal/ » log.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 991     1     1                 1     1           168 168 168         1         151         1         150               1 150 150 18   18 18   18                               18 18 1     150       1             1        
import {Env} from '../env';
import {MessageContext} from '../log';
import {Constructor} from '../providers';
import {joinFqn} from '../util';
 
 
export const BASE_ROOT = 'ts-base';
 
let internalLog: InternalLog;
 
type InternalLogOptions = {
  parent?: string | { fqn: string };
  enable: { warn: boolean; debug: boolean; table: boolean };
  console?: Console;
}
const defaultOptions: InternalLogOptions = { enable: { table: false, warn: false, debug: false } };
 
 
export class InternalLog {
  readonly fqn: string;
 
  protected readonly console: Console;
 
 
  constructor(readonly name: string, readonly options: InternalLogOptions = defaultOptions) {
    this.fqn = joinFqn(name, options.parent);
    this.console = options.console || console;
  }
 
 
  warn(message: string, ...args: unknown[]): void {
    this.options.enable.warn && this.console.warn(`${this.fqn}: ${message}`, ...args);
  }
 
 
  debug(message: string, ...args: unknown[]): void {
    this.options.enable.debug && this.console.debug(`${this.fqn}: ${message}`, ...args);
  }
 
 
  table(tabularData: any, properties?: string[]): void {
    this.options.enable.table && this.console.table(tabularData, properties);
  }
 
 
  extend(name): InternalLog {
    return new InternalLog(name, {
      ...this.options,
      parent: this,
    });
  }
}
 
 
export function logger(name: string | { name: string } | Constructor<any>, _console?: Console): InternalLog {
  let log: InternalLog = internalLog;
  if (!log) {
    const options = { ...defaultOptions, console: _console };
 
    const env = Env.root();
    const enable = env?.getProp(BASE_ROOT) || 'false';
 
    switch (enable) {
      case 'true':
        options.enable = { table: true, warn: true, debug: true };
        break;
      case 'warn':
        options.enable = { table: false, warn: true, debug: true };
        break;
      case 'debug':
        options.enable = { table: false, warn: false, debug: true };
        break;
      case 'table':
        options.enable = { table: true, warn: false, debug: false };
        break;
    }
 
 
    log = new InternalLog(BASE_ROOT, options);
    if (env) {
      internalLog = log;
    }
  }
  return log.extend(name);
}
 
 
export function debug(name: string): (message: string | MessageContext, ...args: unknown[]) => void {
  const log = logger(name);
  const debugFn = log.debug;
  return debugFn.bind(log);
}
 
 
export const MSG_FMT = {
  annotatingClass: 'Annotating Class %s with @%s(%o)',
  annotatingMethod: 'Annotating Method %s.%s with @%s(%o)',
};