Coloured logger

Basic logger with some stylings

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/576832/1817140/Coloured%20logger.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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;
})();