cli.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. 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, _toPropertyKey(descriptor.key), descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  6. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  7. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  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. _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. return CliFormat;
  46. }();
  47. /*
  48. * function cli (opts)
  49. * Returns a new instance of the CLI format that turns a log
  50. * `info` object into the same format previously available
  51. * in `winston.cli()` in `winston < 3.0.0`.
  52. */
  53. module.exports = function (opts) {
  54. return new CliFormat(opts);
  55. };
  56. //
  57. // Attach the CliFormat for registration purposes
  58. //
  59. module.exports.Format = CliFormat;