2023-11-19 17:09:50 +00:00
|
|
|
class Logger {
|
|
|
|
|
static #enabled = true;
|
|
|
|
|
static #lastMessage = '';
|
|
|
|
|
static #spamIndex = 0;
|
|
|
|
|
static #defaultOptions = new Map([
|
|
|
|
|
['spamThreshold', 5],
|
|
|
|
|
['namespace', ''],
|
|
|
|
|
['receiver', 'console'], // can be 'console' or object/function
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables the logger.
|
|
|
|
|
*/
|
|
|
|
|
static enable() {
|
|
|
|
|
Logger.toggle(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disables the logger.
|
|
|
|
|
*/
|
|
|
|
|
static disable() {
|
|
|
|
|
Logger.toggle(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static toggle(value = !Logger.#enabled) {
|
|
|
|
|
if (typeof value !== 'boolean')
|
|
|
|
|
throw new TypeError('value must be a boolean');
|
|
|
|
|
Logger.#enabled = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the default options for the Logger.
|
|
|
|
|
* @param {Map} options - The options to set.
|
|
|
|
|
* @throws {TypeError} If options is not an instance of Map.
|
|
|
|
|
*/
|
|
|
|
|
static setDefaultOptions(options) {
|
|
|
|
|
if (!(options instanceof Map))
|
|
|
|
|
throw new TypeError('options must be a Map');
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of Logger.#defaultOptions.entries()) {
|
|
|
|
|
if (
|
|
|
|
|
key === 'receiver' &&
|
|
|
|
|
!!options.get(key) &&
|
|
|
|
|
value != options.get(key) &&
|
|
|
|
|
!['function', 'object'].includes(typeof options.get(key))
|
|
|
|
|
) {
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
'receiver must be one of: `console`, `function`, `object`'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Logger.#defaultOptions.set(key, options.get(key) ?? value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Logs a message with the specified level and options.
|
|
|
|
|
*
|
2024-01-11 23:24:54 +00:00
|
|
|
* @param {array} data - The message to be logged.
|
2023-11-19 17:09:50 +00:00
|
|
|
* @param {string} level - The log level (e.g., 'info', 'warn', 'error').
|
|
|
|
|
* @param {object} [options] - The optional log options.
|
|
|
|
|
* @param {number} [options.spamThreshold] - The spam threshold for duplicate messages.
|
|
|
|
|
* @param {string} [options.namespace] - The namespace for the log message.
|
|
|
|
|
*/
|
2024-01-11 23:24:54 +00:00
|
|
|
static #log(data, level, options = Logger.#defaultOptions) {
|
2023-11-19 17:09:50 +00:00
|
|
|
if (!Logger.#enabled) return;
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
spamThreshold = Logger.#defaultOptions.spamThreshold,
|
|
|
|
|
namespace,
|
|
|
|
|
} = options;
|
|
|
|
|
|
2024-01-11 23:24:54 +00:00
|
|
|
if (Logger.#lastMessage === data) {
|
2023-11-19 17:09:50 +00:00
|
|
|
if (spamThreshold <= Logger.#spamIndex) return;
|
|
|
|
|
Logger.#spamIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof console[level] === 'function') {
|
|
|
|
|
console[level](
|
2024-01-11 23:24:54 +00:00
|
|
|
`[${namespace ? namespace + ' - ' : ''}${level.toUpperCase()}]`,
|
|
|
|
|
...data
|
2023-11-19 17:09:50 +00:00
|
|
|
);
|
|
|
|
|
} else {
|
2024-01-11 23:24:54 +00:00
|
|
|
console.log(`[${namespace ? namespace + ' - ' : ''}LOG]`, ...data);
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-11 23:24:54 +00:00
|
|
|
if (Logger.#lastMessage !== data) this.#spamIndex = 0;
|
|
|
|
|
Logger.#lastMessage = data;
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-11 23:24:54 +00:00
|
|
|
* Logs an error.
|
2023-11-19 17:09:50 +00:00
|
|
|
*
|
2024-01-11 23:24:54 +00:00
|
|
|
* @param {string} data - The error data to log.
|
2023-11-19 17:09:50 +00:00
|
|
|
*/
|
2024-01-11 23:24:54 +00:00
|
|
|
static error() {
|
|
|
|
|
Logger.#log([...arguments], 'error');
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-11 23:24:54 +00:00
|
|
|
* Logs an information.
|
2023-11-19 17:09:50 +00:00
|
|
|
*
|
2024-01-11 23:24:54 +00:00
|
|
|
* @param {string} data - The data to be logged.
|
2023-11-19 17:09:50 +00:00
|
|
|
*/
|
2024-01-11 23:24:54 +00:00
|
|
|
static info() {
|
|
|
|
|
Logger.#log([...arguments], 'info');
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-11 23:24:54 +00:00
|
|
|
* Logs a warning.
|
2023-11-19 17:09:50 +00:00
|
|
|
*
|
2024-01-11 23:24:54 +00:00
|
|
|
* @param {string} data - The warning data to be logged.
|
2023-11-19 17:09:50 +00:00
|
|
|
*/
|
2024-01-11 23:24:54 +00:00
|
|
|
static warn() {
|
|
|
|
|
Logger.#log([...arguments], 'warn');
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-11 23:24:54 +00:00
|
|
|
* wrapper for logging warnings.
|
2023-11-19 17:09:50 +00:00
|
|
|
*/
|
2024-01-11 23:24:54 +00:00
|
|
|
static warning() {
|
|
|
|
|
Logger.#log([...arguments], 'warn');
|
2023-11-19 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Logger;
|