json.js 705 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. const format = require('./format');
  3. const { MESSAGE } = require('triple-beam');
  4. const jsonStringify = require('fast-safe-stringify');
  5. /*
  6. * function replacer (key, value)
  7. * Handles proper stringification of Buffer output.
  8. */
  9. function replacer(key, value) {
  10. return value instanceof Buffer
  11. ? value.toString('base64')
  12. : value;
  13. }
  14. /*
  15. * function json (info)
  16. * Returns a new instance of the JSON format that turns a log `info`
  17. * object into pure JSON. This was previously exposed as { json: true }
  18. * to transports in `winston < 3.0.0`.
  19. */
  20. module.exports = format((info, opts) => {
  21. info[MESSAGE] = jsonStringify(info, opts.replacer || replacer, opts.space);
  22. return info;
  23. });