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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
 
const os = require('os');
const utils = require('../utils');
const levels = require('../level');
 
const ENABLED = Symbol('Transport#enabled');
 
/**
 * Transport is an output channel of the log that can be output to a file, console or service.
 * A {@link Logger} can configure multiple Transports to meet a variety of complex needs
 */
class Transport {
 
  /**
   * @class
   * @param  {Object} options
   * - {String} [level = NONE] - log level. ouput method must higher than this option. if level is `info`, `debug` will disabled
   * - {Function} formatter - format function
   * - {Function} contextFormatter - format function for context logger
   * - {Boolean} [json = false] - log format is json or not
   * - {String} [encoding = utf8] - log encodeing, see {@link https://github.com/ashtuchkin/iconv-lite#supported-encodings}
   * - {String} [eol = os.EOL] - end of line
   */
  constructor(options) {
    this.options = utils.assign(this.defaults, options);
    if (this.options.encoding === 'utf-8') {
      this.options.encoding = 'utf8';
    }
    this.options.level = utils.normalizeLevel(this.options.level);
    this[ENABLED] = true;
  }
 
  get defaults() {
    return {
      level: 'NONE',
      formatter: null,
      contextFormatter: null,
      json: false,
      encoding: 'utf8',
      eol: os.EOL,
    };
  }
 
  /**
   * enable or not
   * @return {[type]} [description]
   */
  get enabled() {
    return this[ENABLED];
  }
 
  /**
   * enable transport
   */
  enable() {
    this[ENABLED] = true;
  }
 
  /**
   * disable transport
   */
  disable() {
    this[ENABLED] = false;
  }
 
  set level(level) {
    this.options.level = utils.normalizeLevel(level);
  }
 
  get level() {
    return this.options.level;
  }
 
  /**
   * should  output log or not
   * @param  {String} level log level, must in upper case
   * @return {Boolean} should or not
   */
  shouldLog(level) {
    if (!this[ENABLED]) {
      return false;
    }
 
    if (this.options.level === levels['NONE']) {
      return false;
    }
 
    return this.options.level <= levels[level];
  }
 
  /**
   * Transport log method
   * @param  {String} level - log level
   * @param  {Array} args - all methods
   * @param  {Object} meta - meta infomations
   * @return {Buffer|String} log message
   *   - empty string means no log
   *   - utf8 encoding return String
   *   - other encoding return Buffer
   */
  log(level, args, meta) {
    return utils.format(level, args, meta, this.options);
  }
 
  /**
   * reload Transport
   */
  reload() {}
 
  /**
   * close Transport
   */
  close() {}
  end() {}
}
 
module.exports = Transport;