Greasy Fork 还支持 简体中文。

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.你的镜像域名.com/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.

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

Advertisement:

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!)

Advertisement:

// ==UserScript==
// @name        Coloured logger
// @namespace   https://你的镜像域名.com/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;
})();