@nekolab/hanime
Version:
Fast and efficient hanime.tv API wrapper written in TypeScript.
96 lines (95 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggingClient = void 0;
const constants_1 = require("../constants");
/**
* A client for logging.
*/
class LoggingClient {
/**
* Logging configuration.
*/
config;
/**
* Mapping of logging levels to their priorities.
*/
static LEVEL_PRIORITIES = {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
};
/**
* Initializes a new instance of the LoggingClient class.
* @param config Logging configuration.
*/
constructor(config) {
this.config = config;
}
/**
* Log a message at the 'fatal' level.
* @param args Arguments to log.
*/
fatal(...args) {
this.log('fatal', ...args);
}
/**
* Log a message at the 'error' level.
* @param args Arguments to log.
*/
error(...args) {
this.log('error', ...args);
}
/**
* Log a message at the 'warn' level.
* @param args Arguments to log.
*/
warn(...args) {
this.log('warn', ...args);
}
/**
* Log a message at the 'info' level.
* @param args Arguments to log.
*/
info(...args) {
this.log('info', ...args);
}
/**
* Log a message at the 'debug' level.
* @param args Arguments to log.
*/
debug(...args) {
this.log('debug', ...args);
}
/**
* Log a message at the specified level if the current configuration allows it.
* @param level Log level.
* @param args Arguments to log.
*/
log(level, ...args) {
if (this.config.enabled && this.shouldLog(level)) {
console.log(this.getPrefix(level), ...args);
}
}
/**
* Determine if a message should be logged based on the current log level.
* @param level Log level.
* @returns Whether the message should be logged.
*/
shouldLog(level) {
return (LoggingClient.LEVEL_PRIORITIES[level] <=
LoggingClient.LEVEL_PRIORITIES[this.config.level]);
}
/**
* Get the prefix for a log message.
* @param level Log level.
* @returns The log message prefix.
*/
getPrefix(level) {
return (`${constants_1.COLOR.GRAY}[${new Date().toISOString()}] ` +
`[${constants_1.LEVEL_COLORS[level]}${level.toUpperCase()}${constants_1.COLOR.GRAY}]:` +
`${constants_1.COLOR.RESET}`);
}
}
exports.LoggingClient = LoggingClient;