UNPKG

@axiomhq/logging

Version:
55 lines (54 loc) 1.81 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import { LogLevelValue, LogLevel } from "../logger.js"; class SimpleFetchTransport { constructor(config) { __publicField(this, "fetchConfig"); __publicField(this, "events", []); __publicField(this, "timer", null); __publicField(this, "log", (logs) => { const filteredLogs = logs.filter( (log) => LogLevelValue[log.level ?? LogLevel.info] >= LogLevelValue[this.fetchConfig.logLevel ?? LogLevel.info] ); this.events.push(...filteredLogs); if (typeof this.fetchConfig.autoFlush === "undefined" || this.fetchConfig.autoFlush === false) { return; } if (this.timer) { clearTimeout(this.timer); } const flushDelay = typeof this.fetchConfig.autoFlush === "boolean" ? 2e3 : this.fetchConfig.autoFlush.durationMs; this.timer = setTimeout(() => { this.flush(); }, flushDelay); }); this.fetchConfig = config; } async flush() { if (this.events.length <= 0) { return; } await fetch(this.fetchConfig.input, { method: "POST", headers: { "Content-Type": "application/json" }, ...this.fetchConfig.init, body: JSON.stringify(this.events) }).then(async (res) => { if (!res.ok) { console.error(await res.text()); throw new Error("Failed to flush logs"); } this.events = []; }).catch(console.error); } } export { SimpleFetchTransport }; //# sourceMappingURL=fetch.js.map