metadata.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  5. 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); }
  6. var format = require('./format');
  7. function fillExcept(info, fillExceptKeys, metadataKey) {
  8. var savedKeys = fillExceptKeys.reduce(function (acc, key) {
  9. acc[key] = info[key];
  10. delete info[key];
  11. return acc;
  12. }, {});
  13. var metadata = Object.keys(info).reduce(function (acc, key) {
  14. acc[key] = info[key];
  15. delete info[key];
  16. return acc;
  17. }, {});
  18. Object.assign(info, savedKeys, _defineProperty({}, metadataKey, metadata));
  19. return info;
  20. }
  21. function fillWith(info, fillWithKeys, metadataKey) {
  22. info[metadataKey] = fillWithKeys.reduce(function (acc, key) {
  23. acc[key] = info[key];
  24. delete info[key];
  25. return acc;
  26. }, {});
  27. return info;
  28. }
  29. /**
  30. * Adds in a "metadata" object to collect extraneous data, similar to the metadata
  31. * object in winston 2.x.
  32. */
  33. module.exports = format(function (info) {
  34. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  35. var metadataKey = 'metadata';
  36. if (opts.key) {
  37. metadataKey = opts.key;
  38. }
  39. var fillExceptKeys = [];
  40. if (!opts.fillExcept && !opts.fillWith) {
  41. fillExceptKeys.push('level');
  42. fillExceptKeys.push('message');
  43. }
  44. if (opts.fillExcept) {
  45. fillExceptKeys = opts.fillExcept;
  46. }
  47. if (fillExceptKeys.length > 0) {
  48. return fillExcept(info, fillExceptKeys, metadataKey);
  49. }
  50. if (opts.fillWith) {
  51. return fillWith(info, opts.fillWith, metadataKey);
  52. }
  53. return info;
  54. });