logger.class.ts 914 B

12345678910111213141516171819202122232425262728293031
  1. export type LogLevel = 'ERROR' | 'WARNING' | 'INFO' | 'DEBUG';
  2. export class Logger {
  3. public static get LOG_LEVELS(): Array<LogLevel> {
  4. return ['ERROR', 'WARNING', 'INFO', 'DEBUG'];
  5. }
  6. private static levels: Array<LogLevel>;
  7. public static set logLevel(logLevel: LogLevel) {
  8. Logger.levels = Logger.LOG_LEVELS.slice(0, Logger.LOG_LEVELS.indexOf(logLevel) + 1);
  9. }
  10. public static get logLevel(): LogLevel {
  11. return Logger.levels[Logger.levels.length - 1];
  12. }
  13. public static debug(...data: any[]) {
  14. if (Logger.levels.includes('DEBUG')) console.log(...data);
  15. }
  16. public static info(...data: any[]) {
  17. if (Logger.levels.includes('INFO')) console.log(...data);
  18. }
  19. public static warn(...data: any[]) {
  20. if (Logger.levels.includes('WARNING')) console.warn(...data);
  21. }
  22. public static error(...data: any[]) {
  23. if (Logger.levels.includes('ERROR')) console.error(...data);
  24. }
  25. }