simple.js 977 B

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