2019-06-19 21:24:05 +02:00
|
|
|
/**
|
|
|
|
* Simple logger for the lib and plugins.
|
|
|
|
*
|
|
|
|
* @module Logger
|
|
|
|
* @version 0.1.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of logging types.
|
|
|
|
*/
|
|
|
|
export const LogTypes = {
|
|
|
|
/** Alias for error */
|
|
|
|
err: "error",
|
|
|
|
error: "error",
|
|
|
|
/** Alias for debug */
|
|
|
|
dbg: "debug",
|
|
|
|
debug: "debug",
|
|
|
|
log: "log",
|
|
|
|
warn: "warn",
|
|
|
|
info: "info"
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class Logger {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs an error using a collapsed error group with stacktrace.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {string} message - Message or error to have logged.
|
2020-07-25 10:22:57 +02:00
|
|
|
* @param {Error} error - Error object to log with the message.
|
2019-06-19 21:24:05 +02:00
|
|
|
*/
|
|
|
|
static stacktrace(module, message, error) {
|
2020-07-25 10:22:57 +02:00
|
|
|
console.error(`%c[${module}]%c ${message}\n\n%c`, "color: #3a71c1; font-weight: 700;", "color: red; font-weight: 700;", "color: red;", error);
|
2019-06-19 21:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs using error formatting. For logging an actual error object consider {@link module:Logger.stacktrace}
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {string} message - Messages to have logged.
|
|
|
|
*/
|
2020-07-25 10:22:57 +02:00
|
|
|
static err(module, ...message) {Logger._log(module, message, "error");}
|
2019-06-19 21:24:05 +02:00
|
|
|
|
2020-10-06 00:37:17 +02:00
|
|
|
/**
|
|
|
|
* Alias for "err"
|
|
|
|
* @param {string} module NAme of the calling module
|
|
|
|
* @param {...any} message Messages to have logged.
|
|
|
|
*/
|
|
|
|
static error(module, ...message) {Logger._log(module, message, "error");}
|
|
|
|
|
2019-06-19 21:24:05 +02:00
|
|
|
/**
|
|
|
|
* Logs a warning message.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {...any} message - Messages to have logged.
|
|
|
|
*/
|
2020-07-25 10:22:57 +02:00
|
|
|
static warn(module, ...message) {Logger._log(module, message, "warn");}
|
2019-06-19 21:24:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs an informational message.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {...any} message - Messages to have logged.
|
|
|
|
*/
|
2020-07-25 10:22:57 +02:00
|
|
|
static info(module, ...message) {Logger._log(module, message, "info");}
|
2019-06-19 21:24:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs used for debugging purposes.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {...any} message - Messages to have logged.
|
|
|
|
*/
|
2020-07-25 10:22:57 +02:00
|
|
|
static debug(module, ...message) {Logger._log(module, message, "debug");}
|
2019-06-19 21:24:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs used for basic loggin.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {...any} message - Messages to have logged.
|
|
|
|
*/
|
2020-07-25 10:22:57 +02:00
|
|
|
static log(module, ...message) {Logger._log(module, message);}
|
2019-06-19 21:24:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs strings using different console levels and a module label.
|
|
|
|
*
|
|
|
|
* @param {string} module - Name of the calling module.
|
|
|
|
* @param {any|Array<any>} message - Messages to have logged.
|
|
|
|
* @param {module:Logger.LogTypes} type - Type of log to use in console.
|
|
|
|
*/
|
|
|
|
static _log(module, message, type = "log") {
|
|
|
|
type = Logger.parseType(type);
|
|
|
|
if (!Array.isArray(message)) message = [message];
|
2020-07-27 00:47:04 +02:00
|
|
|
console[type](`%c[BetterDiscord]%c [${module}]%c`, "color: #3E82E5; font-weight: 700;", "color: #3a71c1;", "", ...message);
|
2019-06-19 21:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static parseType(type) {
|
2020-07-23 22:51:58 +02:00
|
|
|
return LogTypes[type] || "log";
|
2019-06-19 21:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|