Coloured logger

Basic logger with some stylings

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.greatest.deepsurf.us/scripts/576832/1817140/Coloured%20logger.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        Coloured logger
// @namespace   https://greatest.deepsurf.us/en/users/1574555-littux
// @version     1.0.0
// @description Basic logger with some stylings
// @author      littux
// @grant       none
// @license     GPL-3.0-only
// @run-at      document-start
// ==/UserScript==

(() => {
	const globals = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;
	globals.__littuxUserscripts__ ??= {};
	const userScripts = globals.__littuxUserscripts__;

	userScripts.logger ??= {
		logFuncMap: {
			debug: console.debug,
			info: console.info,
			log: console.log,
			warn: console.warn,
			error: console.error,
			crit: console.error,
		},
		logBGColorMap: {
			debug: "#555",
			info: "#0cf",
			log: "#fff",
			warn: "#ff0",
			error: "#f00",
			crit: "#700",
		},
		logColorMap: {
			debug: "#fff",
			info: "#000",
			log: "#000",
			warn: "#000",
			error: "#fff",
			crit: "#fff",
		},

		log({ level = "log", msg, func }, ...args) {
			userScripts.logger.logFuncMap[level](
				"%c[" + func + "] %c" + level + "%c " + msg,
				"font-weight: bold",
				"font-weight: bold; padding: 1px 3px; border-radius: 4px; background: " + userScripts.logger.logBGColorMap[level] + "; color: " + userScripts.logger.logColorMap[level],
				"",
				...args
			);
		},

		Logger: class Logger {
			constructor(funcName) {
				this.funcName = funcName;
				this.logMsg = userScripts.logger.log;
			}

			debug(msg, ...args) {
				this.logMsg({ level: "debug", msg, func: this.funcName }, ...args);
			}

			info(msg, ...args) {
				this.logMsg({ level: "info", msg, func: this.funcName }, ...args);
			}

			log(msg, ...args) {
				this.logMsg({ level: "log", msg, func: this.funcName }, ...args);
			}

			warn(msg, ...args) {
				this.logMsg({ level: "warn", msg, func: this.funcName }, ...args);
			}

			error(msg, ...args) {
				this.logMsg({ level: "error", msg, func: this.funcName }, ...args);
			}

			crit(msg, ...args) {
				this.logMsg({ level: "crit", msg, func: this.funcName }, ...args);
			}
		},

		getLogger(funcName) { return new userScripts.logger.Logger(funcName) }
	};

	window.getLogger = userScripts.logger.getLogger;
})();