simple.js 1020 B

123456789101112131415161718192021222324252627282930313233343536
  1. /* eslint no-undefined: 0 */
  2. 'use strict';
  3. var format = require('./format');
  4. var _require = require('triple-beam'),
  5. MESSAGE = _require.MESSAGE;
  6. var jsonStringify = require('fast-safe-stringify');
  7. /*
  8. * function simple (info)
  9. * Returns a new instance of the simple format TransformStream
  10. * which writes a simple representation of logs.
  11. *
  12. * const { level, message, splat, ...rest } = info;
  13. *
  14. * ${level}: ${message} if rest is empty
  15. * ${level}: ${message} ${JSON.stringify(rest)} otherwise
  16. */
  17. module.exports = format(function (info) {
  18. var stringifiedRest = jsonStringify(Object.assign({}, info, {
  19. level: undefined,
  20. message: undefined,
  21. splat: undefined
  22. }));
  23. var padding = info.padding && info.padding[info.level] || '';
  24. if (stringifiedRest !== '{}') {
  25. info[MESSAGE] = info.level + ':' + padding + ' ' + info.message + ' ' + stringifiedRest;
  26. } else {
  27. info[MESSAGE] = info.level + ':' + padding + ' ' + info.message;
  28. }
  29. return info;
  30. });