easy-logger

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

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/443079/1038070/easy-logger.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         easy-logger
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @license      MIT
// @description  簡易的なロギングユーティリティです。
// @author       You
// ==/UserScript==

class EasyLogger {
  static #levels = {
    all: { priority: 0, color: 'grey' },
    trace: { priority: 1, color: 'blue' },
    debug: { priority: 2, color: 'cyan' },
    info: { priority: 3, color: 'green' },
    warn: { priority: 4, color: 'yellow' },
    error: { priority: 5, color: 'red' },
    fatal: { priority: 6, color: 'magenta' },
    mark: { priority: 7, color: 'grey' },
    off: { priority: 8 }
  }
  #category = 'default'

  setCategory(category) {
    this.#category = category

    return this
  }

  #level = 'all'
  #levelPriority = 0

  setLevel(level) {
    this.#level = level
    this.#levelPriority = this.constructor.#levels[level].priority

    return this
  }

  static #colorReset = '\u001b[0m'
  static #colorMap = {
    'grey': '\u001b[90m',
    'blue': '\u001b[34m',
    'cyan': '\u001b[36m',
    'green': '\u001b[32m',
    'yellow': '\u001b[33m',
    'red': '\u001b[31m',
    'magenta': '\u001b[35m'
  }

  static #colorText(text, color) {
    return `${this.#colorMap[color]}${text}${this.#colorReset}`
  }

  #createLog(text, level) {
    return this.constructor.#colorText(
      `[${new Date().toISOString().slice(0, -1)}] [${level.toUpperCase()}] ${this.#category} - ${text}`,
      this.constructor.#levels[level].color
    )
  }

  #log(text, level) {
    if (this.#levelPriority <= this.constructor.#levels[level].priority) {
      console.log(this.#createLog(text, level))
    }

    return this
  }

  trace(text) {
    return this.#log(text, 'trace')
  }

  debug(text) {
    return this.#log(text, 'debug')
  }

  info(text) {
    return this.#log(text, 'info')
  }

  warn(text) {
    return this.#log(text, 'warn')
  }

  error(text) {
    return this.#log(text, 'error')
  }

  fatal(text) {
    return this.#log(text, 'fatal')
  }

  mark(text) {
    return this.#log(text, 'mark')
  }
}

window.EasyLogger = EasyLogger