metadata.js 1.6 KB

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