JaBa/helpers/logger.js
Jonny_Bro (Nikita) c3dca64be4 prettier
2023-07-05 00:58:06 +05:00

60 lines
1.4 KiB
JavaScript

const { bgBlue, black, green } = require("chalk");
function dateTimePad(value, digits) {
let number = value;
while (number.toString().length < digits) number = "0" + number;
return number;
}
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)
);
}
module.exports = class Logger {
static log(content, type = "log") {
const date = `[${format(new Date(Date.now()))}]:`;
switch (type) {
case "log": {
return console.log(`${date} ${bgBlue(type.toUpperCase())} ${content} `);
}
case "warn": {
return console.log(`${date} ${black.bgYellow(type.toUpperCase())} ${content} `);
}
case "error": {
return console.log(`${date} ${black.bgRed(type.toUpperCase())} ${content} `);
}
case "debug": {
return console.log(`${date} ${green(type.toUpperCase())} ${content} `);
}
case "cmd": {
return console.log(`${date} ${black.bgWhite(type.toUpperCase())} ${content}`);
}
case "ready": {
return console.log(`${date} ${black.bgGreen(type.toUpperCase())} ${content}`);
}
default:
throw new TypeError("Logger type must be either warn, debug, log, ready, cmd or error.");
}
}
};