All files / src logger.ts

76% Statements 19/25
37.5% Branches 3/8
83.33% Functions 5/6
90% Lines 18/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 531x   1x               1x 1x   1x 5x 5x 5x           1x             1x 1x       15x 15x       77x 77x       93x       93x 93x      
import { config } from './config'
 
const levels = [
  'debug',
  'info',
  'warn',
  'error',
  'none'
]
 
const levelToInt: {[level: string]: number} = {}
const intToLevel: {[int: number]: string} = {}
 
for (let index = 0; index < levels.length; index++) {
  const level = levels[index]
  levelToInt[level] = index
  intToLevel[index] = level
}
 
/**
* @ignore
*/
export class Logger {
  static error(message: string) {
    if (!this.shouldLog('error')) return
    console.error(this.logMessage('error', message))
  }
 
  static warn(message: string) {
    Iif (!this.shouldLog('warn')) return
    console.warn(this.logMessage('warn', message))
  }
 
  static info(message: string) {
    Iif (!this.shouldLog('info')) return
    console.log(this.logMessage('info', message))
  }
 
  static debug(message: string) {
    Iif (!this.shouldLog('debug')) return
    console.log(this.logMessage('debug', message))
  }
 
  static logMessage(level: string, message: string) {
    return `[${level.toUpperCase()}] ${message}`
  }
 
  static shouldLog(level: string) {
    const currentLevel = levelToInt[config.logLevel]
    return currentLevel <= levelToInt[level]
  }
}