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";
function dateTimePad(value, digits) {
let number = value;
while (number.toString().length < digits) number = "0" + number;
return number;
}
import chalk from "chalk";
function format(tDate) {
return (
dateTimePad(tDate.getDate(), 2) +
"-" +
dateTimePad(tDate.getMonth() + 1, 2) +
"-" +
dateTimePad(tDate.getFullYear(), 2) +
" " +
dateTimePad(tDate.getHours(), 2) +
":" +
dateTimePad(tDate.getMinutes(), 2) +
":" +
dateTimePad(tDate.getSeconds(), 2) +
"." +
dateTimePad(tDate.getMilliseconds(), 3)
);
return new Intl.DateTimeFormat("ru-RU", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}).format(tDate);
}
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 {
log(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${bgBlue("LOG")} ${content}`);
return console.log(`[${format(Date.now())}]: ${logLevels.LOG} ${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) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgRed("ERROR")} ${content}`);
return console.log(`[${format(Date.now())}]: ${logLevels.ERROR} ${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) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgWhite("CMD")} ${content}`);
return console.log(`[${format(Date.now())}]: ${logLevels.CMD} ${content}`);
},
ready(content) {
return console.log(`[${format(new Date(Date.now()))}]: ${black.bgGreen("READY")} ${content}`);
return console.log(`[${format(Date.now())}]: ${logLevels.READY} ${content}`);
},
};