-
Notifications
You must be signed in to change notification settings - Fork 10
/
Sample.transport.ts
57 lines (47 loc) · 1.44 KB
/
Sample.transport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as Transport from "winston-transport";
import { clone } from "@/utils";
// Batching adapted from https://github.com/winstonjs/winston/blob/master/lib/winston/transports/http.js
export class SampleTransport extends Transport {
// Batch
private batchEntries: any[] = [];
private batchInterval = 2000;
private batchCount = 100;
private batchTimeoutID = -1;
constructor(opts = {}) {
super(opts);
}
log(info: any, callback: () => void): void {
setImmediate(callback);
const cb = (err?: any) => {
if (err) {
console.error(err);
this.emit("warn", err);
} else {
this.emit("logged", info);
}
};
this.batchEntries.push(info);
if (this.batchEntries.length === 1) {
// @ts-expect-error Similar type?
this.batchTimeoutID = setTimeout(() => {
this.batchTimeoutID = -1;
this.doBatchRequest(cb);
}, this.batchInterval);
} else if (this.batchEntries.length >= this.batchCount) {
this.doBatchRequest(cb);
}
}
doBatchRequest(cb: (err?: any) => void) {
// Reset timeout ID
if (this.batchTimeoutID > 0) {
clearTimeout(this.batchTimeoutID);
this.batchTimeoutID = -1;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const entriesCopy = clone(this.batchEntries);
this.batchEntries = [];
// TODO Send logs to remote
// console.log(entriesCopy.map(e => e.message));
cb();
}
}