UNPKG

@zencemarketing/web-sdk

Version:

ZenceMarketing Web SDK for push notifications, popups, and custom event tracking.

69 lines (68 loc) 2.06 kB
export class SDKLogger { constructor(config) { this.level = config.level; this.remoteLogging = config.remoteLogging || false; this.remoteUrl = config.remoteUrl; this.sdkVersion = config.sdkVersion; } setLevel(level) { this.level = level; } setEnabled(enabled) { this.level = enabled ? this.level : "none"; } setRemoteLogging(enabled) { this.remoteLogging = enabled; } shouldLog(level) { if (this.level === "none") return false; const order = ["error", "warn", "info", "debug"]; return order.indexOf(level) <= order.indexOf(this.level); } sendRemoteLog(level, ...args) { if (this.remoteLogging && this.remoteUrl) { try { fetch(this.remoteUrl, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ level, message: args, sdkVersion: this.sdkVersion, timestamp: new Date().toISOString(), }), }); } catch (e) { console.warn("Remote log failed:", e); } } } error(...args) { if (this.shouldLog("error")) { console.error("[SDK ERROR]", ...args); this.sendRemoteLog("error", ...args); } } warn(...args) { if (this.shouldLog("warn")) { console.warn("[SDK WARN]", ...args); this.sendRemoteLog("warn", ...args); } } info(...args) { if (this.shouldLog("info")) { console.info("[SDK INFO]", ...args); this.sendRemoteLog("info", ...args); } } debug(...args) { if (this.shouldLog("debug")) { console.debug("[SDK DEBUG]", ...args); this.sendRemoteLog("debug", ...args); } } }