templates/components/messenger/js/dev/logger.js.twig line 1

Open in your IDE?
  1. {% set options = {
  2. }|merge(options|default({})) %}
  3. class Logger {
  4.     constructor({
  5.                     enabled = false,
  6.                     level = "info",
  7.                     name = null,
  8.     } = {}) {
  9.         this.enabled = enabled;
  10.         this.level = level;
  11.         this.name = name;
  12.         this.levels = {
  13.             error: 0,
  14.             warn: 1,
  15.             info: 2,
  16.             debug: 3
  17.         };
  18.     }
  19.     log(level, ...args) {
  20.         if (!this.enabled) return;
  21.         if (this.levels[level] <= this.levels[this.level]) {
  22.             let text = `[${level}]`;
  23.             if (this.name) {
  24.                 text += ` [${this.name}]`;
  25.             }
  26.             if (level === "error") {
  27.                 console.error(text, ...args);
  28.             } else {
  29.                 console.log(text, ...args);
  30.             }
  31.         }
  32.     }
  33.     error(...args) {
  34.         this.log("error", ...args);
  35.         this.logErrorObjects(args);
  36.     }
  37.     warn(...args)  {
  38.         this.log("warn", ...args);
  39.     }
  40.     info(...args)  {
  41.         this.log("info", ...args);
  42.     }
  43.     debug(...args) {
  44.         this.log("debug", ...args);
  45.     }
  46.     logErrorObjects(args) {
  47.         for (const arg of args) {
  48.             if (arg instanceof Error) {
  49.                 console.error(arg);
  50.             }
  51.         }
  52.     }
  53. }