Coloured logger

Basic logger with some stylings

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.greasyfork.org/scripts/576832/1817140/Coloured%20logger.js

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

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.

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

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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://greasyfork.org/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;
})();