logger.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * This file is part of the Forge extension for GNOME
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. import { production } from "./settings.js";
  19. export class Logger {
  20. static #settings;
  21. static LOG_LEVELS = {
  22. OFF: 0,
  23. FATAL: 1,
  24. ERROR: 2,
  25. WARN: 3,
  26. INFO: 4,
  27. DEBUG: 5,
  28. TRACE: 6,
  29. ALL: 7,
  30. };
  31. static init(settings) {
  32. this.#settings = settings;
  33. }
  34. static get #level() {
  35. if (this.#settings?.get_boolean?.("logging-enabled")) {
  36. return production
  37. ? Logger.LOG_LEVELS.OFF
  38. : this.#settings?.get_uint?.("log-level") ?? Logger.LOG_LEVELS.OFF;
  39. }
  40. return Logger.LOG_LEVELS.OFF;
  41. }
  42. // TODO: use console.* methods
  43. static format(msg, ...params) {
  44. return params.reduce((acc, val) => acc.replace("{}", val), msg);
  45. }
  46. static fatal(...args) {
  47. if (this.#level > Logger.LOG_LEVELS.OFF) log(`[Forge] [FATAL]`, ...args);
  48. }
  49. static error(...args) {
  50. if (this.#level > Logger.LOG_LEVELS.FATAL) log(`[Forge] [ERROR]`, ...args);
  51. }
  52. static warn(...args) {
  53. if (this.#level > Logger.LOG_LEVELS.ERROR) log(`[Forge] [WARN]`, ...args);
  54. }
  55. static info(...args) {
  56. if (this.#level > Logger.LOG_LEVELS.WARN) log(`[Forge] [INFO]`, ...args);
  57. }
  58. static debug(...args) {
  59. if (this.#level > Logger.LOG_LEVELS.INFO) log(`[Forge] [DEBUG]`, ...args);
  60. }
  61. static trace(...args) {
  62. if (this.#level > Logger.LOG_LEVELS.DEBUG) log(`[Forge] [TRACE]`, ...args);
  63. }
  64. static log(...args) {
  65. if (this.#level > Logger.LOG_LEVELS.OFF) log(`[Forge] [LOG]`, ...args);
  66. }
  67. }