@dima_aryze/reforge
Version:
TypeScript/JavaScript SDK for Reforge - Cross-chain token operations
141 lines • 3.83 kB
JavaScript
/**
* Professional logging utility for the Reforge SDK
*/
/**
* Log levels mapped to numeric values for comparison
*/
const LOG_LEVELS = {
error: 0,
warn: 1,
info: 2,
debug: 3,
};
/**
* Professional logger implementation with configurable levels and formatting
*/
export class Logger {
constructor(config) {
Object.defineProperty(this, "config", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.config = {
level: config.level,
prefix: config.prefix ?? '[REFORGE SDK]',
timestamps: config.timestamps ?? true,
customLogger: config.customLogger,
};
}
/**
* Check if a log level should be output based on current threshold
*/
shouldLog(level) {
return LOG_LEVELS[level] <= LOG_LEVELS[this.config.level];
}
/**
* Format a log message with prefix and timestamp
*/
formatMessage(level, message) {
const timestamp = this.config.timestamps ? `[${new Date().toISOString()}]` : '';
const prefix = this.config.prefix;
const levelTag = `[${level.toUpperCase()}]`;
return [timestamp, prefix, levelTag, message].filter(Boolean).join(' ');
}
/**
* Log an error message
*/
error(message, ...args) {
if (!this.shouldLog('error'))
return;
const formattedMessage = this.formatMessage('error', message);
if (this.config.customLogger?.error) {
this.config.customLogger.error(formattedMessage, ...args);
}
else {
console.error(formattedMessage, ...args);
}
}
/**
* Log a warning message
*/
warn(message, ...args) {
if (!this.shouldLog('warn'))
return;
const formattedMessage = this.formatMessage('warn', message);
if (this.config.customLogger?.warn) {
this.config.customLogger.warn(formattedMessage, ...args);
}
else {
console.warn(formattedMessage, ...args);
}
}
/**
* Log an info message
*/
info(message, ...args) {
if (!this.shouldLog('info'))
return;
const formattedMessage = this.formatMessage('info', message);
if (this.config.customLogger?.info) {
this.config.customLogger.info(formattedMessage, ...args);
}
else {
// eslint-disable-next-line no-console
console.info(formattedMessage, ...args);
}
}
/**
* Log a debug message
*/
debug(message, ...args) {
if (!this.shouldLog('debug'))
return;
const formattedMessage = this.formatMessage('debug', message);
if (this.config.customLogger?.debug) {
this.config.customLogger.debug(formattedMessage, ...args);
}
else {
// eslint-disable-next-line no-console
console.debug(formattedMessage, ...args);
}
}
/**
* Update the log level
*/
setLevel(level) {
this.config.level = level;
}
/**
* Get the current log level
*/
getLevel() {
return this.config.level;
}
/**
* Create a child logger with modified configuration
*/
child(config) {
return new Logger({
...this.config,
...config,
});
}
}
/**
* Create a default logger instance
*/
export function createLogger(level = 'info', prefix) {
return new Logger({ level, prefix });
}
/**
* Silent logger that doesn't output anything - useful for testing
*/
export const silentLogger = {
error: () => { },
warn: () => { },
info: () => { },
debug: () => { },
};
//# sourceMappingURL=logger.js.map