Code coverage report for node/src/log/StdOutLog.ts

Statements: 74.07% (20 / 27)      Branches: 65% (13 / 20)      Functions: 75% (6 / 8)      Lines: 74.07% (20 / 27)      Ignored: none     

All files » node/src/log/ » StdOutLog.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 1461                     1                                                   1                         7 7 7         2                       2         1                                                 4                 4 4           4 4       4               2 2 2 1     1                      
import {
  Env,
  Log,
  LogConfig,
  LogLevel,
  LogOptions,
  Message,
  MessageContext,
  LOG_ENV,
} from '@btilford/ts-base-core';
import {Writable} from 'stream';
import util from 'util';
 
 
export type StdOutOptions = {
  /**
   * Will default to `process.stdout`
   */
  stdout?: Writable;
  /**
   * Will default to `process.stderr`
   */
  stderr?: Writable;
}
 
export type StdLogOptions = StdOutOptions & LogOptions & {
  parent?: StdOutLog;
};
 
 
/**
 * Log that writes to a `Writable`. By default `process.stdout` and `process.stderr` are used.
 *
 * When formatting the message by default `util.format` is used but you can set `LogConfig.messageFormat` to use something
 * like `printj.sprintf` as well.
 *
 */
export class StdOutLog extends Log {
  protected readonly stdout: Writable;
  protected readonly stderr: Writable;
 
 
  protected constructor(
    name: string,
    config: LogConfig,
    parent?: Log,
    env?: Env,
    stdout?: Writable,
    stderr?: Writable,
  ) {
    super(name, config, parent);
    this.stdout = stdout || process.stdout;
    this.stderr = stderr || process.stderr;
  }
 
 
  static create(options: StdLogOptions): StdOutLog {
    return new StdOutLog(
      options.name || '',
      LogConfig.create(options.config),
      options.parent,
      options.env,
      options.stdout,
      options.stderr,
    );
  }
 
 
  extend(name: string, config?: LogConfig): StdOutLog {
    return new StdOutLog(name, config || this.config, this, this.env, this.stdout, this.stderr);
  }
 
 
  copy(name: string, config?: LogConfig): StdOutLog {
    return new StdOutLog(name, config || this.config, undefined, this.env, this.stdout, this.stderr);
  }
 
 
  /**
   * This will create a `Console` that prints the table to `this.stdout`.
   * @param tabularData
   * @param properties
   */
  table(tabularData: any, properties: string[]): void {
    if (this.enabled(LOG_ENV.table)) {
      const tmp = new console.Console(this.stdout, this.stderr);
      tmp.table(tabularData, properties);
    }
  }
 
 
  /**
   *
   * @param level
   * @param msg
   * @param args
   */
  protected createOutputString(level: LogLevel, msg: MessageContext, ...args: unknown[]): string {
    let formatStr: string;
    const context = new MessageContext(msg.message, {
      ...this.config.contextSupplier(),
      name: this.name,
      fqn: this.fqn,
      level,
      ...msg.data,
      args,
    });
 
    Eif (this.config.messageTemplate) {
      formatStr = this.config.messageTemplate.format(context);
    }
    else {
      formatStr = context.message;
    }
 
    Eif (this.config.interceptors) {
      this.config.interceptors.forEach(interceptor => {
        interceptor(new Message(level, context));
      });
    }
    return this.config.messageFormat ?
           this.config.messageFormat(formatStr, ...args) :
           util.format(formatStr, ...args);
  }
 
 
  protected _write(level: LogLevel, message: MessageContext, ...args: unknown[]): void | Error {
 
    try {
      const output = this.createOutputString(level, message, ...args);
      if (level.gte(LogLevel.WARN)) {
        this.stderr.write(output);
      }
      else {
        this.stdout.write(output);
      }
 
    }
    catch (error) {
      console.error('Error writing message', message);
      return error;
    }
  }
}