console.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const Transport = require('./transport');
  3. const utils = require('../utils');
  4. const levels = require('../level');
  5. /**
  6. * output log to console {@link Transport}。
  7. * specifical level by EGG_LOG has the highest priority
  8. */
  9. class ConsoleTransport extends Transport {
  10. /**
  11. * @class
  12. * @param {Object} options
  13. * - {Array} [stderrLevel = ERROR] - output to stderr level, must higher than options.level
  14. */
  15. constructor(options) {
  16. super(options);
  17. this.options.stderrLevel = utils.normalizeLevel(this.options.stderrLevel);
  18. // EGG_LOG has the highest priority
  19. if (process.env.EGG_LOG) {
  20. this.options.level = utils.normalizeLevel(process.env.EGG_LOG);
  21. }
  22. }
  23. get defaults() {
  24. return utils.assign(super.defaults, {
  25. stderrLevel: 'ERROR',
  26. });
  27. }
  28. /**
  29. * output log, see {@link Transport#log}
  30. * if stderrLevel presents, will output log to stderr
  31. * @param {String} level - log level, in upper case
  32. * @param {Array} args - all arguments
  33. * @param {Object} meta - meta infomations
  34. */
  35. log(level, args, meta) {
  36. const msg = super.log(level, args, meta);
  37. if (levels[level] >= this.options.stderrLevel && levels[level] < levels['NONE']) {
  38. process.stderr.write(msg);
  39. } else {
  40. process.stdout.write(msg);
  41. }
  42. }
  43. }
  44. module.exports = ConsoleTransport;