averox-sdk-secure
Version:
Real-time SDK for key monitoring, secure messaging, and A/V streaming with encryption
119 lines (118 loc) • 3.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.LogLevel = void 0;
/**
* Log levels supported by the logger
*/
var LogLevel;
(function (LogLevel) {
LogLevel["DEBUG"] = "DEBUG";
LogLevel["INFO"] = "INFO";
LogLevel["WARN"] = "WARN";
LogLevel["ERROR"] = "ERROR";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
/**
* Logger class for consistent logging across the SDK
*/
class Logger {
/**
* Creates a new Logger instance
* @param {LoggerConfig} config - Logger configuration
*/
constructor(config) {
this.config = {
timestamps: true,
showLevel: true,
...config,
};
}
/**
* Log a debug message
* @param {string} message - Message to log
* @param {unknown[]} args - Additional arguments
*/
debug(message, ...args) {
this.log(LogLevel.DEBUG, message, ...args);
}
/**
* Log an info message
* @param {string} message - Message to log
* @param {unknown[]} args - Additional arguments
*/
info(message, ...args) {
this.log(LogLevel.INFO, message, ...args);
}
/**
* Log a warning message
* @param {string} message - Message to log
* @param {unknown[]} args - Additional arguments
*/
warn(message, ...args) {
this.log(LogLevel.WARN, message, ...args);
}
/**
* Log an error message
* @param {string} message - Message to log
* @param {unknown[]} args - Additional arguments
*/
error(message, ...args) {
this.log(LogLevel.ERROR, message, ...args);
}
/**
* Internal logging method
* @param {LogLevel} level - Log level
* @param {string} message - Message to log
* @param {unknown[]} args - Additional arguments
*/
log(level, message, ...args) {
if (this.shouldLog(level)) {
const formattedMessage = this.formatMessage(level, message);
if (this.config.logHandler) {
this.config.logHandler(level, formattedMessage, ...args);
return;
}
switch (level) {
case LogLevel.ERROR:
// eslint-disable-next-line no-console
console.error(formattedMessage, ...args);
break;
case LogLevel.WARN:
// eslint-disable-next-line no-console
console.warn(formattedMessage, ...args);
break;
default:
// eslint-disable-next-line no-console
console.log(formattedMessage, ...args);
}
}
}
/**
* Check if message should be logged based on minimum level
* @param {LogLevel} level - Log level to check
* @returns {boolean} Whether message should be logged
*/
shouldLog(level) {
const levels = Object.values(LogLevel);
const minLevelIndex = levels.indexOf(this.config.minLevel);
const currentLevelIndex = levels.indexOf(level);
return currentLevelIndex >= minLevelIndex;
}
/**
* Format log message with timestamp and level if configured
* @param {LogLevel} level - Log level
* @param {string} message - Message to format
* @returns {string} Formatted message
*/
formatMessage(level, message) {
const parts = [];
if (this.config.timestamps) {
parts.push(`[${new Date().toISOString()}]`);
}
if (this.config.showLevel) {
parts.push(`[${level}]`);
}
parts.push(message);
return parts.join(' ');
}
}
exports.Logger = Logger;