HiddenPhox/src/lib/logger.js

79 lines
1.4 KiB
JavaScript
Raw Normal View History

const ESCAPE = "\x1b";
const NORMAL = "[3";
const BRIGHT = "[9";
const NORMAL_BG = "[4";
const BRIGHT_BG = "[10";
const NAMES = [
"Black",
"Red",
"Green",
"Yellow",
"Blue",
"Magenta",
"Cyan",
"White",
];
const COLORS = {};
for (const index in NAMES) {
COLORS[NAMES[index].toUpperCase()] = index;
}
COLORS.DEFAULT = 9;
function color(name, bright = false, bg = false) {
name = name.toUpperCase();
let out = "" + ESCAPE;
if (bg) {
if (bright && name != "DEFAULT") {
out += BRIGHT_BG;
} else {
out += NORMAL_BG;
}
} else {
if (bright && name != "DEFAULT") {
out += BRIGHT;
} else {
out += NORMAL;
}
}
out += COLORS[name] + "m";
return out;
}
const LEVEL_COLORS = {
silly: ["magenta", true],
verbose: ["green", true],
debug: ["cyan", true],
log: ["blue", false],
info: ["blue", false],
warn: ["yellow", false],
error: ["red", false],
};
2022-10-09 18:42:52 +00:00
function baseLogger(prefix, level, ...message) {
level = level || "info";
let out = "";
const [col, bright] = LEVEL_COLORS[level];
out += `${color(col, bright)}[${level.toUpperCase()}] `;
out += `${color("magenta")}[${prefix}] ${color("default")}`;
2022-10-09 18:46:18 +00:00
out += [...message].map((m) => m?.toString()).join(" ");
console.log(out);
}
module.exports = {};
for (const level in LEVEL_COLORS) {
2022-10-09 18:44:34 +00:00
module.exports[level] = (prefix, ...message) =>
baseLogger.apply(null, [prefix, level, ...message]);
}