/ lib / utils / test_logger.js
test_logger.js
 1  require('colors');
 2  
 3  // TODO: just logFunction changes, probably doesn't need a whole new module just
 4  // for this
 5  class TestLogger {
 6    constructor(options) {
 7      this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
 8      this.logLevel = options.logLevel || 'info';
 9    }
10  
11    logFunction() {
12      console.log(...arguments);
13    }
14  
15    error(txt) {
16      if (!(this.shouldLog('error'))) {
17        return;
18      }
19      this.logFunction(txt.red);
20    }
21  
22    warn(txt) {
23      if (!(this.shouldLog('warn'))) {
24        return;
25      }
26      this.logFunction(txt.yellow);
27    }
28  
29    info(txt) {
30      if (!(this.shouldLog('info'))) {
31        return;
32      }
33      this.logFunction(txt.green);
34    }
35  
36    debug(txt) {
37      if (!(this.shouldLog('debug'))) {
38        return;
39      }
40      this.logFunction(txt);
41    }
42  
43    trace(txt) {
44      if (!(this.shouldLog('trace'))) {
45        return;
46      }
47      this.logFunction(txt);
48    }
49  
50    shouldLog(level) {
51      return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
52    }
53  
54  }
55  
56  module.exports = TestLogger;