wisdom-sdk
Version:
Core business logic and data access layer for prediction markets
94 lines (91 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
// src/logger.ts
var createLogger = () => {
const logLevel = process.env.LOG_LEVEL || "info";
const logLevels = {
debug: 0,
info: 1,
warn: 2,
error: 3
};
const currentLevelValue = logLevel in logLevels ? logLevels[logLevel] : logLevels.info;
const formatLog = (level, obj, msg) => {
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
const service = "wisdom-sdk";
const objStr = JSON.stringify(obj);
return `[${timestamp}] ${level.toUpperCase()} [${service}] ${msg || ""} ${objStr}`;
};
return {
debug: (obj, msg) => {
if (currentLevelValue <= 0) {
console.debug(formatLog("debug", obj, msg));
}
},
info: (obj, msg) => {
if (currentLevelValue <= 1) {
console.info(formatLog("info", obj, msg));
}
},
warn: (obj, msg) => {
if (currentLevelValue <= 2) {
console.warn(formatLog("warn", obj, msg));
}
},
error: (obj, msg) => {
if (currentLevelValue <= 3) {
console.error(formatLog("error", obj, msg));
}
},
child: (bindings) => {
const childLogger = createLogger();
return {
debug: (obj, msg) => childLogger.debug({ ...obj, ...bindings }, msg),
info: (obj, msg) => childLogger.info({ ...obj, ...bindings }, msg),
warn: (obj, msg) => childLogger.warn({ ...obj, ...bindings }, msg),
error: (obj, msg) => childLogger.error({ ...obj, ...bindings }, msg),
child: (nestedBindings) => childLogger.child({ ...bindings, ...nestedBindings })
};
}
};
};
var logger = createLogger();
function getContextLogger(context) {
return logger.child({ context });
}
var AppError = class extends Error {
constructor({
message,
context = "general",
code = "INTERNAL_ERROR",
originalError,
data
}) {
super(message);
this.name = "AppError";
this.context = context;
this.code = code;
this.originalError = originalError;
this.data = data;
Error.captureStackTrace(this, this.constructor);
}
// Logs this error with appropriate context and returns it
log() {
const contextLogger = getContextLogger(this.context);
const logObj = {
code: this.code,
error: this.message,
...this.originalError && { originalError: this.originalError.message },
...this.data && { data: this.data }
};
contextLogger.error(logObj, this.message);
return this;
}
};
var logger_default = logger;
exports.AppError = AppError;
exports.default = logger_default;
exports.getContextLogger = getContextLogger;
exports.logger = logger;
//# sourceMappingURL=logger.cjs.map
//# sourceMappingURL=logger.cjs.map