woomy-v2/bot/util/logger.js

93 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-10-20 05:47:05 +00:00
/**
* Colorful logger class from the Chariot.js Client framework featuring various log levels.
* Link: https://github.com/riyacchi/chariot.js/master/helpers/Logger.js
*/
const chalk = require('chalk');
class Logger {
/**
* Pads a single number for unified looks in the console
* @param {number} number The number that should be force-padded
* @returns {number} The padded number
*/
static _forcePadding (number) {
return (number < 10 ? '0' : '') + number;
}
/**
* Gets the full current system time and date for logging purposes
* @returns {string} The formatted current time
*/
static _getCurrentTime () {
const now = new Date();
const day = this._forcePadding(now.getDate());
const month = this._forcePadding(now.getMonth() + 1);
const year = this._forcePadding(now.getFullYear());
const hour = this._forcePadding(now.getHours());
const minute = this._forcePadding(now.getMinutes());
const second = this._forcePadding(now.getSeconds());
return `${day}.${month}.${year} ${hour}:${minute}:${second}`;
}
/**
* Log something related to being successful
* @param {string} title The title of the log enty
* @param {string} body The body of the log entry
* @returns {void}
*/
static success (title, body) {
console.log(chalk.bold.green(`[ ${this._getCurrentTime()} ] [ ${title} ] `) + body);
}
/**
* Log something related to a warning
* @param {string} title The title of the log enty
* @param {string} body The body of the log entry
* @returns {void}
*/
static warning (title, body) {
console.log(chalk.bold.yellow(`[ ${this._getCurrentTime()} ] [ ${title} ] `) + body);
}
/**
* Log something related to an error
* @param {string} title The title of the log enty
* @param {string} body The body of the log entry
* @returns {void}
*/
static error (title, body) {
console.log(chalk.bold.red(`[ ${this._getCurrentTime()} ] [ ${title} ] `) + body);
}
/**
* Log something related to debugging
* @param {string} title The title of the log enty
* @param {string} body The body of the log entry
* @returns {void}
*/
static debug (title, body) {
console.log(chalk.bold.magenta(`[ ${this._getCurrentTime()} ] [ ${title} ] `) + body);
}
/**
* Log something related to an event
* @param {string} body The body of the log entry
* @returns {void}
*/
static event (body) {
2020-10-20 08:30:46 +00:00
console.log(chalk.bold.blue(`[ ${this._getCurrentTime()} ] [ EVENT ] `) + body);
2020-10-20 05:47:05 +00:00
}
/**
* Log something related to command usage
* @param {string} body The body of the log entry
* @returns {void}
*/
static command (body) {
2020-10-21 07:07:03 +00:00
console.log(chalk.bold.white(`[ ${this._getCurrentTime()} ] [ COMMAND ] `) + body);
2020-10-20 05:47:05 +00:00
}
}
module.exports = Logger;