refactor: logger

This commit is contained in:
Slincnik 2024-12-08 18:00:26 +03:00
parent dc7bef73ee
commit cfee30528d
No known key found for this signature in database

View file

@ -1,52 +1,47 @@
import { bgBlue, black, green } from "chalk"; import chalk from "chalk";
function dateTimePad(value, digits) {
let number = value;
while (number.toString().length < digits) number = "0" + number;
return number;
}
function format(tDate) { function format(tDate) {
return ( return new Intl.DateTimeFormat("ru-RU", {
dateTimePad(tDate.getDate(), 2) + year: "numeric",
"-" + month: "2-digit",
dateTimePad(tDate.getMonth() + 1, 2) + day: "2-digit",
"-" + hour: "2-digit",
dateTimePad(tDate.getFullYear(), 2) + minute: "2-digit",
" " + second: "2-digit",
dateTimePad(tDate.getHours(), 2) + }).format(tDate);
":" +
dateTimePad(tDate.getMinutes(), 2) +
":" +
dateTimePad(tDate.getSeconds(), 2) +
"." +
dateTimePad(tDate.getMilliseconds(), 3)
);
} }
const logLevels = {
LOG: chalk.bgBlue("LOG"),
WARN: chalk.black.bgYellow("WARN"),
ERROR: chalk.black.bgRed("ERROR"),
DEBUG: chalk.green("DEBUG"),
CMD: chalk.black.bgWhite("CMD"),
READY: chalk.black.bgGreen("READY"),
};
export default { export default {
log(content) { log(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${bgBlue("LOG")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.LOG} ${content}`);
}, },
warn(content) { warn(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgYellow("WARN")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.WARN} ${content}`);
}, },
error(content) { error(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgRed("ERROR")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.ERROR} ${content}`);
}, },
debug(content) { debug(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${green("DEBUG")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.DEBUG} ${content}`);
}, },
cmd(content) { cmd(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgWhite("CMD")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.CMD} ${content}`);
}, },
ready(content) { ready(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgGreen("READY")} ${content}`); return console.log(`[${format(Date.now())}]: ${logLevels.READY} ${content}`);
}, },
}; };