forked from OutOfSyncStudios/winston-logstash-transport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (31 loc) · 1.26 KB
/
index.js
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
"use strict";
const LogstashTransport = require("./LogstashTransport");
class LogstashTransportMiddleware {
constructor(options) {
const { middlewareOptions, ...rest } = options;
middlewareOptions.refreshCount = 0;
middlewareOptions.options = rest;
this.middlewareOptions = middlewareOptions;
return new LogstashTransport({ ...rest, refreshTransport: this.refreshTransport.bind(this) });
}
refreshTransport() {
const { maxRetries, timeoutMaxRetries, logger, options, refreshCount, refreshTime } = this.middlewareOptions;
if (refreshCount <= maxRetries) {
logger.remove(logger.transports.find(transport => transport.name === "logstashTransport"));
setTimeout(() => {
// Update Refresh Count
this.middlewareOptions = {
...this.middlewareOptions,
refreshCount: refreshCount + 1,
refreshTime: refreshTime || new Date()
}
// Replace Transport
logger.add(new LogstashTransport({ ...options, refreshTransport: this.refreshTransport.bind(this) }));
logger.error({
message:`Logstash Transport Restarted. Lost logs between now and ${this.middlewareOptions.refreshTime}`
});
}, timeoutMaxRetries);
}
}
}
module.exports = LogstashTransportMiddleware;