From cfee30528d69335b57b3a572cc98c189c6a327e5 Mon Sep 17 00:00:00 2001 From: Slincnik Date: Sun, 8 Dec 2024 18:00:26 +0300 Subject: [PATCH] refactor: logger --- helpers/logger.js | 53 +++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/helpers/logger.js b/helpers/logger.js index 4c912908..b987c48a 100644 --- a/helpers/logger.js +++ b/helpers/logger.js @@ -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}`); }, };