@axiomhq/winston
Version:
The official Axiom transport for winston logger
68 lines (64 loc) • 1.84 kB
JavaScript
;
var Transport = require('winston-transport');
var js = require('@axiomhq/js');
class WinstonTransport extends Transport {
client;
dataset;
batch = [];
batchCallback = () => { };
batchTimeoutId;
constructor(opts) {
super(opts);
this.client = new js.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);
}
}
exports.WinstonTransport = WinstonTransport;
//# sourceMappingURL=index.cjs.map