All files / src logger.ts

60% Statements 21/35
11.11% Branches 1/9
50% Functions 5/10
60% Lines 21/35

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 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                      8x 8x 8x 8x 8x 8x                       8x                           8x 24x 24x 24x 24x   23x   1054x           272x           107x                                   24x 24x           8x 8x   8x  
/*
 * The coLAB project
 * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
 *
 * Licensed under the MIT License
 */
 
type LogFn = (...args: unknown[]) => void;
 
export type LoggerLevel = 0 | 1 | 2 | 3 | 4 | 5;
 
export const OFF: LoggerLevel = 0;
export const ERROR: LoggerLevel = 1;
export const WARN: LoggerLevel = 2;
export const INFO: LoggerLevel = 3;
export const DEBUG: LoggerLevel = 4;
export const TRACE: LoggerLevel = 5;
 
interface Logger {
  getLevel: () => LoggerLevel;
  setLevel: (level: LoggerLevel) => void;
  trace: LogFn;
  debug: LogFn;
  info: LogFn;
  warn: LogFn;
  error: LogFn;
}
 
export const loggers: Record<string, Logger> = {};
 
function mapArgs(...args: unknown[]): unknown[] {
  return args.map(arg => {
    const argP = typeof arg === 'function' ? arg() : arg;
    try {
      return argP;
      //return typeof argP === 'object' ? JSON.stringify(argP) : arg;
    } catch {
      return arg;
    }
  });
}
 
export function getLogger(name: string): Logger {
  const logger = loggers[name];
  if (logger == null) {
    let currentLevel: LoggerLevel = WARN;
    const logger: Logger = {
      getLevel: () => currentLevel,
      setLevel: (level: LoggerLevel) => (currentLevel = level),
      trace: (...params: unknown[]): void => {
        Iif (currentLevel >= TRACE) {
          // eslint-disable-next-line no-console
          console.info(...mapArgs(...params));
        }
      },
      debug: (...params: unknown[]): void => {
        Iif (currentLevel >= DEBUG) {
          // eslint-disable-next-line no-console
          console.info(...mapArgs(...params));
        }
      },
      info: (...params: unknown[]): void => {
        Iif (currentLevel >= INFO) {
          // eslint-disable-next-line no-console
          console.info(...mapArgs(...params));
        }
      },
      warn: (...params: unknown[]): void => {
        Iif (currentLevel >= WARN) {
          // eslint-disable-next-line no-console
          console.warn(...mapArgs(...params));
        }
      },
      error: (...params: unknown[]): void => {
        Iif (currentLevel >= ERROR) {
          // eslint-disable-next-line no-console
          console.error(...mapArgs(...params));
        }
      },
    };
    loggers[name] = logger;
    return logger;
  } else E{
    return logger;
  }
}
 
const logger = getLogger('default');
logger.setLevel(INFO);
 
export default logger;