This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cetra/lib/core/Logger.js

126 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

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.
*
* @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.
*/
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;
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](
`[${namespace ? namespace + ' - ' : ''}${level.toUpperCase()}]`,
...data
2023-11-19 17:09:50 +00:00
);
} else {
console.log(`[${namespace ? namespace + ' - ' : ''}LOG]`, ...data);
2023-11-19 17:09:50 +00:00
}
if (Logger.#lastMessage !== data) this.#spamIndex = 0;
Logger.#lastMessage = data;
2023-11-19 17:09:50 +00:00
}
/**
* Logs an error.
2023-11-19 17:09:50 +00:00
*
* @param {string} data - The error data to log.
2023-11-19 17:09:50 +00:00
*/
static error() {
Logger.#log([...arguments], 'error');
2023-11-19 17:09:50 +00:00
}
/**
* Logs an information.
2023-11-19 17:09:50 +00:00
*
* @param {string} data - The data to be logged.
2023-11-19 17:09:50 +00:00
*/
static info() {
Logger.#log([...arguments], 'info');
2023-11-19 17:09:50 +00:00
}
/**
* Logs a warning.
2023-11-19 17:09:50 +00:00
*
* @param {string} data - The warning data to be logged.
2023-11-19 17:09:50 +00:00
*/
static warn() {
Logger.#log([...arguments], 'warn');
2023-11-19 17:09:50 +00:00
}
/**
* wrapper for logging warnings.
2023-11-19 17:09:50 +00:00
*/
static warning() {
Logger.#log([...arguments], 'warn');
2023-11-19 17:09:50 +00:00
}
}
export default Logger;