cli.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const { Colorizer } = require('./colorize');
  3. const { Padder } = require('./pad-levels');
  4. const { configs, MESSAGE } = require('triple-beam');
  5. /**
  6. * Cli format class that handles initial state for a a separate
  7. * Colorizer and Padder instance.
  8. */
  9. class CliFormat {
  10. constructor(opts = {}) {
  11. if (!opts.levels) {
  12. opts.levels = configs.npm.levels;
  13. }
  14. this.colorizer = new Colorizer(opts);
  15. this.padder = new Padder(opts);
  16. this.options = opts;
  17. }
  18. /*
  19. * function transform (info, opts)
  20. * Attempts to both:
  21. * 1. Pad the { level }
  22. * 2. Colorize the { level, message }
  23. * of the given `logform` info object depending on the `opts`.
  24. */
  25. transform(info, opts) {
  26. this.colorizer.transform(
  27. this.padder.transform(info, opts),
  28. opts
  29. );
  30. info[MESSAGE] = `${info.level}:${info.message}`;
  31. return info;
  32. }
  33. }
  34. /*
  35. * function cli (opts)
  36. * Returns a new instance of the CLI format that turns a log
  37. * `info` object into the same format previously available
  38. * in `winston.cli()` in `winston < 3.0.0`.
  39. */
  40. module.exports = opts => new CliFormat(opts);
  41. //
  42. // Attach the CliFormat for registration purposes
  43. //
  44. module.exports.Format = CliFormat;