cli.js 2.8 KB

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