@factorial-finance/blueprint-node
Version:
blueprint-node-plugin
221 lines (220 loc) • 9.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const chalk_1 = __importDefault(require("chalk"));
const defaults_1 = require("../constants/defaults");
class Logger {
constructor(options = {}) {
this.options = {
level: (process.env.NODE_ENV === 'test' ? 'silent' : 'info'),
format: 'simple',
timestamp: true,
colors: true,
...options
};
}
// Singleton getInstance method
static getInstance(options = {}) {
if (!Logger.instance) {
Logger.instance = new Logger(options);
}
return Logger.instance;
}
// Configure logger with initial options (call this before first use)
static configure(options) {
if (Logger.instance) {
// If instance already exists, update its options
Logger.instance.updateOptions(options);
}
else {
// Create new instance with provided options
Logger.instance = new Logger(options);
}
return Logger.instance;
}
// Update logger options
updateOptions(options) {
this.options = {
...this.options,
...options
};
}
// Get current options
getOptions() {
return { ...this.options };
}
info(message, ...args) {
this.log('info', message, ...args);
}
error(message, ...args) {
this.log('error', message, ...args);
}
warn(message, ...args) {
this.log('warn', message, ...args);
}
debug(message, ...args) {
this.log('debug', message, ...args);
}
log(level, message, ...args) {
if (!this.shouldLog(level)) {
return;
}
const timestamp = this.options.timestamp ? this.getTimestamp() : '';
const coloredLevel = this.options.colors ? this.colorizeLevel(level) : level.toUpperCase();
const formattedMessage = this.options.colors ? this.colorizeMessage(message, level) : message;
const logParts = [
timestamp && chalk_1.default.gray(`[${timestamp}]`),
coloredLevel,
formattedMessage,
...args
].filter(Boolean);
console.log(logParts.join(' '));
}
shouldLog(level) {
const currentLevel = this.options.level || 'info';
if (currentLevel === 'silent') {
return false;
}
const levels = ['debug', 'info', 'warn', 'error'];
const currentLevelIndex = levels.indexOf(currentLevel);
const messageLevelIndex = levels.indexOf(level);
return messageLevelIndex >= currentLevelIndex;
}
getTimestamp() {
return new Date().toISOString().replace('T', ' ').substring(0, 19);
}
colorizeLevel(level) {
switch (level) {
case 'info': return chalk_1.default.blue('[INFO]');
case 'error': return chalk_1.default.red('[ERROR]');
case 'warn': return chalk_1.default.yellow('[WARN]');
case 'debug': return chalk_1.default.gray('[DEBUG]');
default: return `[${level.toUpperCase()}]`;
}
}
colorizeMessage(message, level) {
switch (level) {
case 'error': return chalk_1.default.red(message);
case 'warn': return chalk_1.default.yellow(message);
case 'info': return chalk_1.default.white(message);
case 'debug': return chalk_1.default.gray(message);
default: return message;
}
}
// Static methods for direct usage
static info(message, ...args) {
Logger.getInstance().info(message, ...args);
}
static error(message, ...args) {
Logger.getInstance().error(message, ...args);
}
static warn(message, ...args) {
Logger.getInstance().warn(message, ...args);
}
static debug(message, ...args) {
Logger.getInstance().debug(message, ...args);
}
static log(level, message, ...args) {
Logger.getInstance().log(level, message, ...args);
}
static print(message, ...args) {
console.log(message, ...args);
}
// Static methods for backward compatibility
static logRPCCall(method, params) {
const logger = Logger.getInstance();
if (!logger.shouldLog('info')) {
return;
}
const timestamp = logger.getTimestamp();
const writeMethods = ['sendBoc', 'setBalance', 'increaseBalance', 'setAccountCode', 'setAccountData'];
const methodFormatted = writeMethods.includes(method) ?
chalk_1.default.red.bold(method) :
chalk_1.default.green.bold(method);
console.log(`${chalk_1.default.gray(`[${timestamp}]`)} ${chalk_1.default.blue('[INFO]')} ${methodFormatted} ${Logger.formatParams(method, params)}`);
}
static formatParams(method, params) {
switch (method) {
case 'getAddressInformation':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))}`;
case 'runGetMethod':
const stackInfo = params.stack ?
chalk_1.default.yellow(`[${params.stack.length} items]`) :
chalk_1.default.gray('[empty]');
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} method=${chalk_1.default.cyan(params.method)} stack=${stackInfo}`;
case 'getTransactions':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} limit=${chalk_1.default.magenta(params.limit || 'default')}`;
case 'sendBoc':
return chalk_1.default.yellow('[BOC message]');
case 'getTransaction':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} lt=${chalk_1.default.magenta(params.lt)}`;
case 'tryLocateResultTx':
case 'tryLocateSourceTx':
return `from=${chalk_1.default.blue(Logger.formatAddress(params.source))} to=${chalk_1.default.blue(Logger.formatAddress(params.destination))} lt=${chalk_1.default.magenta(params.created_lt)}`;
case 'getShards':
return `seqno=${chalk_1.default.magenta(params.seqno)}`;
case 'estimateFee':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))}`;
case 'getBlockTransactions':
return `workchain=${chalk_1.default.magenta(params.workchain)} seqno=${chalk_1.default.magenta(params.seqno)}`;
case 'setBalance':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} balance=${chalk_1.default.magenta(params.balance)}`;
case 'increaseBalance':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} amount=${chalk_1.default.magenta(params.amount)}`;
case 'setAccountCode':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} codeHash=${chalk_1.default.magenta(params.codeHash)}`;
case 'setAccountData':
return `address=${chalk_1.default.blue(Logger.formatAddress(params.address))} dataHash=${chalk_1.default.magenta(params.dataHash)}`;
default:
return chalk_1.default.gray('[no params]');
}
}
static logError(method, error) {
const logger = Logger.getInstance();
if (!logger.shouldLog('error')) {
return;
}
const timestamp = logger.getTimestamp();
const methodFormatted = chalk_1.default.red.bold(method);
const errorMsg = chalk_1.default.red(`ERROR: ${error.message || error}`);
console.log(`${chalk_1.default.gray(`[${timestamp}]`)} ${chalk_1.default.red('[ERROR]')} ${methodFormatted} ${errorMsg}`);
}
static logSeparator() {
const logger = Logger.getInstance();
if (!logger.shouldLog('info')) {
return;
}
console.log(chalk_1.default.gray('─'.repeat(defaults_1.DEFAULTS.LOG_SEPARATOR_LENGTH)));
}
static formatAddress(address) {
return address;
}
// Utility methods for setting log levels
static setLogLevel(level) {
Logger.getInstance().updateOptions({ level });
}
static getLogLevel() {
return Logger.getInstance().getOptions().level || 'info';
}
// Convenience methods for common log level configurations
static enableDebugMode() {
Logger.setLogLevel('debug');
}
static enableInfoMode() {
Logger.setLogLevel('info');
}
static enableSilentMode() {
Logger.setLogLevel('silent');
}
// Check if certain log level is enabled
static isDebugEnabled() {
return Logger.getInstance().shouldLog('debug');
}
static isInfoEnabled() {
return Logger.getInstance().shouldLog('info');
}
}
exports.Logger = Logger;