json.js 1003 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var format = require('./format');
  3. var _require = require('triple-beam'),
  4. MESSAGE = _require.MESSAGE;
  5. var stringify = require('safe-stable-stringify');
  6. /*
  7. * function replacer (key, value)
  8. * Handles proper stringification of Buffer and bigint output.
  9. */
  10. function replacer(key, value) {
  11. // safe-stable-stringify does support BigInt, however, it doesn't wrap the value in quotes.
  12. // Leading to a loss in fidelity if the resulting string is parsed.
  13. // It would also be a breaking change for logform.
  14. if (typeof value === 'bigint') return value.toString();
  15. return value;
  16. }
  17. /*
  18. * function json (info)
  19. * Returns a new instance of the JSON format that turns a log `info`
  20. * object into pure JSON. This was previously exposed as { json: true }
  21. * to transports in `winston < 3.0.0`.
  22. */
  23. module.exports = format(function (info, opts) {
  24. var jsonStringify = stringify.configure(opts);
  25. info[MESSAGE] = jsonStringify(info, opts.replacer || replacer, opts.space);
  26. return info;
  27. });