BetterDiscordApp-rauenzi/common/logger.js

102 lines
3.0 KiB
JavaScript
Raw Normal View History

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.
* @param {Error} error - Error object to log with the message.
2019-06-19 21:24:05 +02:00
*/
static stacktrace(module, message, error) {
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.
*/
static err(module, ...message) {Logger._log(module, message, "error");}
2019-06-19 21:24:05 +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.
*/
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.
*/
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.
*/
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.
*/
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
}
}