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