All files / src/utils logger.js

86.66% Statements 13/15
40% Branches 4/10
85.71% Functions 6/7
90.9% Lines 10/11

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    20x         287x 287x 249x   38x         287x       426x         250x     13x     24x        
import { getEnvironmentType } from './environmentHelper';
 
const LEVELS = {
  debug: 0, info: 1, warn: 2, error: 3,
};
 
function getMinLevel() {
  try {
    const env = getEnvironmentType();
    return env === 'local' ? 'debug' : 'warn';
  } catch {
    return 'debug';
  }
}
 
function shouldLog(level) {
  return LEVELS[level] >= LEVELS[getMinLevel()];
}
 
export default function createLogger(context) {
  return {
    debug(msg, ...args) {
      if (shouldLog('debug')) console.log(`[DEBUG] [${context}]`, msg, ...args); // eslint-disable-line no-console
    },
    info(msg, ...args) {
      Eif (shouldLog('info')) console.log(`[INFO] [${context}]`, msg, ...args); // eslint-disable-line no-console
    },
    warn(msg, ...args) {
      Eif (shouldLog('warn')) console.warn(`[WARN] [${context}]`, msg, ...args); // eslint-disable-line no-console
    },
    error(msg, ...args) {
      Eif (shouldLog('error')) console.error(`[ERROR] [${context}]`, msg, ...args); // eslint-disable-line no-console
    },
  };
}