cli.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. var _require = require('./colorize'),
  5. Colorizer = _require.Colorizer;
  6. var _require2 = require('./pad-levels'),
  7. Padder = _require2.Padder;
  8. var _require3 = require('triple-beam'),
  9. configs = _require3.configs,
  10. MESSAGE = _require3.MESSAGE;
  11. /**
  12. * Cli format class that handles initial state for a a separate
  13. * Colorizer and Padder instance.
  14. */
  15. var CliFormat = function () {
  16. function CliFormat() {
  17. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  18. _classCallCheck(this, CliFormat);
  19. if (!opts.levels) {
  20. opts.levels = configs.npm.levels;
  21. }
  22. this.colorizer = new Colorizer(opts);
  23. this.padder = new Padder(opts);
  24. this.options = opts;
  25. }
  26. /*
  27. * function transform (info, opts)
  28. * Attempts to both:
  29. * 1. Pad the { level }
  30. * 2. Colorize the { level, message }
  31. * of the given `logform` info object depending on the `opts`.
  32. */
  33. _createClass(CliFormat, [{
  34. key: 'transform',
  35. value: function transform(info, opts) {
  36. this.colorizer.transform(this.padder.transform(info, opts), opts);
  37. info[MESSAGE] = info.level + ':' + info.message;
  38. return info;
  39. }
  40. }]);
  41. return CliFormat;
  42. }();
  43. /*
  44. * function cli (opts)
  45. * Returns a new instance of the CLI format that turns a log
  46. * `info` object into the same format previously available
  47. * in `winston.cli()` in `winston < 3.0.0`.
  48. */
  49. module.exports = function (opts) {
  50. return new CliFormat(opts);
  51. };
  52. //
  53. // Attach the CliFormat for registration purposes
  54. //
  55. module.exports.Format = CliFormat;