easy-logger

簡易的なロギングユーティリティです。

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/443079/1038070/easy-logger.js

  1. // ==UserScript==
  2. // @name easy-logger
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.1
  5. // @license MIT
  6. // @description 簡易的なロギングユーティリティです。
  7. // @author You
  8. // ==/UserScript==
  9.  
  10. class EasyLogger {
  11. static #levels = {
  12. all: { priority: 0, color: 'grey' },
  13. trace: { priority: 1, color: 'blue' },
  14. debug: { priority: 2, color: 'cyan' },
  15. info: { priority: 3, color: 'green' },
  16. warn: { priority: 4, color: 'yellow' },
  17. error: { priority: 5, color: 'red' },
  18. fatal: { priority: 6, color: 'magenta' },
  19. mark: { priority: 7, color: 'grey' },
  20. off: { priority: 8 }
  21. }
  22. #category = 'default'
  23.  
  24. setCategory(category) {
  25. this.#category = category
  26.  
  27. return this
  28. }
  29.  
  30. #level = 'all'
  31. #levelPriority = 0
  32.  
  33. setLevel(level) {
  34. this.#level = level
  35. this.#levelPriority = this.constructor.#levels[level].priority
  36.  
  37. return this
  38. }
  39.  
  40. static #colorReset = '\u001b[0m'
  41. static #colorMap = {
  42. 'grey': '\u001b[90m',
  43. 'blue': '\u001b[34m',
  44. 'cyan': '\u001b[36m',
  45. 'green': '\u001b[32m',
  46. 'yellow': '\u001b[33m',
  47. 'red': '\u001b[31m',
  48. 'magenta': '\u001b[35m'
  49. }
  50.  
  51. static #colorText(text, color) {
  52. return `${this.#colorMap[color]}${text}${this.#colorReset}`
  53. }
  54.  
  55. #createLog(text, level) {
  56. return this.constructor.#colorText(
  57. `[${new Date().toISOString().slice(0, -1)}] [${level.toUpperCase()}] ${this.#category} - ${text}`,
  58. this.constructor.#levels[level].color
  59. )
  60. }
  61.  
  62. #log(text, level) {
  63. if (this.#levelPriority <= this.constructor.#levels[level].priority) {
  64. console.log(this.#createLog(text, level))
  65. }
  66.  
  67. return this
  68. }
  69.  
  70. trace(text) {
  71. return this.#log(text, 'trace')
  72. }
  73.  
  74. debug(text) {
  75. return this.#log(text, 'debug')
  76. }
  77.  
  78. info(text) {
  79. return this.#log(text, 'info')
  80. }
  81.  
  82. warn(text) {
  83. return this.#log(text, 'warn')
  84. }
  85.  
  86. error(text) {
  87. return this.#log(text, 'error')
  88. }
  89.  
  90. fatal(text) {
  91. return this.#log(text, 'fatal')
  92. }
  93.  
  94. mark(text) {
  95. return this.#log(text, 'mark')
  96. }
  97. }
  98.  
  99. window.EasyLogger = EasyLogger