UNPKG

@axiomhq/logging

Version:
55 lines (54 loc) 1.93 kB
"use strict"; 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; }; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const logger = require("../logger.cjs"); class SimpleFetchTransport { constructor(config) { __publicField(this, "fetchConfig"); __publicField(this, "events", []); __publicField(this, "timer", null); __publicField(this, "log", (logs) => { const filteredLogs = logs.filter( (log) => logger.LogLevelValue[log.level ?? logger.LogLevel.info] >= logger.LogLevelValue[this.fetchConfig.logLevel ?? logger.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); } } exports.SimpleFetchTransport = SimpleFetchTransport; //# sourceMappingURL=fetch.cjs.map