all files / src/ Logger.js

100% Statements 39/39
100% Branches 12/12
100% Functions 9/9
100% Lines 24/24
1 statement, 1 branch Ignored     
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              100×     17× 17× 11×   11×   17×       126×   37×         21× 12×   19× 13×   21× 12×   105× 89×      
/* eslint-disable no-underscore-dangle */
const LEVELS = {
  error: 50,
  warn: 40,
  info: 30,
  debug: 20,
};
const DEFAULT_LEVEL = LEVELS.info;
 
export default class Logger {
  constructor() {
    this._level = DEFAULT_LEVEL;
  }
 
  level(inVal) {
    let val = inVal;
    if (val) {
      if (typeof val === 'string') {
        val = LEVELS[val];
      }
      this._level = val || DEFAULT_LEVEL;
    }
    return this._level;
  }
 
  // Abstract the console call:
  _log(method, args) {
    if (this._level <= LEVELS[method === 'log' ? 'debug' : method]) {
      /* eslint-disable no-console */
      console[method](...args);
      /* eslint-enable no-console */
    }
  }
 
  error(...args) {
    return this._log('error', args);
  }
  warn(...args) {
    return this._log('warn', args);
  }
  info(...args) {
    return this._log('info', args);
  }
  debug(...args) {
    return this._log('log', args);
  }
}