@axiomhq/logging
Version:
The official logging package for Axiom
49 lines (48 loc) • 1.47 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const logger = require("../logger.cjs");
class SimpleFetchTransport {
fetchConfig;
events = [];
timer = null;
constructor(config) {
this.fetchConfig = config;
}
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);
};
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