All files / packages/logger/src/format/plugins cli.ts

0% Statements 0/22
100% Branches 1/1
100% Functions 1/1
0% Lines 0/22

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                                                                                                               
import type { TransformFunction } from '../type'
import type { ColorizeOptions } from './colorize'
import type { PadLevelsOptions } from './pad-levels'
import { configs, MESSAGE } from '../../triple-beam'
import { Colorizer } from './colorize'
import { Padder } from './pad-levels'
 
type CliFormatOptions = ColorizeOptions & PadLevelsOptions
 
/**
 * Cli format class that handles initial state for a separate
 * Colorizer and Padder instance.
 */
class CliFormat {
  colorizer: Colorizer
  padder: Padder
  options: CliFormatOptions
 
  constructor(opts: CliFormatOptions = {}) {
    if (!opts.levels) {
      opts.levels = configs.cli.levels
    }
 
    this.colorizer = new Colorizer(opts)
    this.padder = new Padder(opts)
    this.options = opts
  }
 
  /*
   * function transform (info, opts)
   * Attempts to both:
   * 1. Pad the { level }
   * 2. Colorize the { level, message }
   * of the given `logform` info object depending on the `opts`.
   */
  transform: TransformFunction<CliFormatOptions> = (info, opts) => {
    this.colorizer.transform(this.padder.transform(info, opts), opts)
 
    info[MESSAGE] = `${info.level}:${info.message}`
    return info
  }
}
 
/*
 * function cli (opts)
 * Returns a new instance of the CLI format that turns a log
 * `info` object into the same format previously available
 * in `winston.cli()` in `winston < 3.0.0`.
 */
export default (opts: CliFormatOptions) => new CliFormat(opts)
 
//
// Attach the CliFormat for registration purposes
//
export { CliFormat as Format }