@aut-labs/sdk
Version:
The TS/JS SDK package aims to make it easy for frontends/backends to integrate with Aut Smart Contracts
83 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogger = void 0;
class Logger {
constructor(options = {}) {
const { type = "pretty", hideLogForProduction = false } = options;
this.type = type;
this.hideLog = hideLogForProduction;
}
static getInstance(options) {
if (!Logger.instance) {
Logger.instance = new Logger(options);
}
return Logger.instance;
}
getFormattedTime() {
const now = new Date();
return now.toISOString();
}
formatMessage(level, message, optionalParams) {
const timestamp = this.getFormattedTime();
const levelStyles = this.getLevelStyles(level);
let logPosition = "";
if (!this.hideLog) {
logPosition = this.getLogPosition();
}
if (this.type === "pretty") {
console.log(`%c[${timestamp}] [${level}]${logPosition}`, levelStyles, message, ...optionalParams);
}
else {
console.log(`[${timestamp}] [${level}]${logPosition}`, message, ...optionalParams);
}
}
getLevelStyles(level) {
switch (level) {
case "INFO":
return "color: SteelBlue; font-weight: bold";
case "WARN":
return "color: GoldenRod; font-weight: bold";
case "ERROR":
return "color: Crimson; font-weight: bold";
case "DEBUG":
return "color: MediumPurple; font-weight: bold";
case "LOG":
default:
return "color: DimGray; font-weight: bold";
}
}
getLogPosition() {
try {
const err = new Error();
const stack = err.stack?.split("\n") || [];
const callerStackLine = stack[4] || stack[3] || "";
const matches = callerStackLine.match(/at\s+(.*)/);
if (matches && matches[1]) {
return ` [${matches[1]}]`;
}
}
catch (e) { }
return "";
}
log(message, ...optionalParams) {
this.formatMessage("LOG", message, optionalParams);
}
info(message, ...optionalParams) {
this.formatMessage("INFO", message, optionalParams);
}
warn(message, ...optionalParams) {
this.formatMessage("WARN", message, optionalParams);
}
error(message, ...optionalParams) {
this.formatMessage("ERROR", message, optionalParams);
}
debug(message, ...optionalParams) {
this.formatMessage("DEBUG", message, optionalParams);
}
}
exports.default = Logger;
const getLogger = (options) => {
return Logger.getInstance(options);
};
exports.getLogger = getLogger;
//# sourceMappingURL=logger.js.map