@axiomhq/winston
Version:
The official Axiom transport for winston logger
66 lines (63 loc) • 1.82 kB
JavaScript
import Transport from 'winston-transport';
import { AxiomWithoutBatching } from '@axiomhq/js';
class WinstonTransport extends Transport {
client;
dataset;
batch = [];
batchCallback = () => { };
batchTimeoutId;
constructor(opts) {
super(opts);
this.client = new AxiomWithoutBatching({
token: opts.token,
orgId: opts.orgId,
url: opts.url,
edge: opts.edge,
edgeUrl: opts.edgeUrl,
onError: opts.onError,
});
this.dataset = opts?.dataset || process.env.AXIOM_DATASET || '';
}
log(info, callback) {
this.request(info, (err) => {
if (err) {
this.emit('error', err);
}
else {
this.emit('logged', info);
}
});
if (callback) {
setImmediate(callback);
}
}
request(info, callback) {
if (!info._time) {
info._time = new Date().toISOString();
}
this.batch.push(info);
if (this.batch.length == 1) {
this.batchCallback = callback;
this.batchTimeoutId = setTimeout(() => {
this.flush();
}, 1000);
callback(null);
}
else if (this.batch.length >= 1000) {
this.flush(callback);
}
}
flush(callback = () => { }) {
const batchCopy = this.batch.slice();
clearTimeout(this.batchTimeoutId);
this.batchTimeoutId = undefined;
this.batchCallback = () => { };
this.batch = [];
this.client
.ingest(this.dataset, batchCopy)
.then((_res) => callback(null))
.catch(callback);
}
}
export { WinstonTransport };
//# sourceMappingURL=index.js.map