json.js 983 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const format = require('./format');
  3. const { MESSAGE } = require('triple-beam');
  4. const stringify = require('safe-stable-stringify');
  5. /*
  6. * function replacer (key, value)
  7. * Handles proper stringification of Buffer and bigint output.
  8. */
  9. function replacer(key, value) {
  10. // safe-stable-stringify does support BigInt, however, it doesn't wrap the value in quotes.
  11. // Leading to a loss in fidelity if the resulting string is parsed.
  12. // It would also be a breaking change for logform.
  13. if (typeof value === 'bigint')
  14. 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((info, opts) => {
  24. const jsonStringify = stringify.configure(opts);
  25. info[MESSAGE] = jsonStringify(info, opts.replacer || replacer, opts.space);
  26. return info;
  27. });