forked from OutOfSyncStudios/winston-logstash-transport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp
171 lines (143 loc) · 3.94 KB
/
temp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Logstash.prototype.log = function (level, msg, meta, callback) {
var self = this,
meta = winston.clone(meta || {}),
log_entry;
for (var property in this.meta_defaults) {
meta[property] = this.meta_defaults[property];
}
if (self.silent) {
return callback(null, true);
}
if (self.strip_colors) {
msg = msg.stripColors;
// Let's get rid of colors on our meta properties too.
if (typeof meta === 'object') {
for (var property in meta) {
meta[property] = meta[property].stripColors;
}
}
}
log_entry = this.transform(level, msg, meta, self);
if (!self.connected) {
self.log_queue.push({
message: log_entry,
callback: function () {
self.emit('logged');
callback(null, true);
}
});
} else {
self.sendLog(log_entry, function () {
self.emit('logged');
callback(null, true);
});
}
};
Logstash.prototype.connect = function () {
var tryReconnect = true;
var options = {};
var self = this;
this.retries++;
this.connecting = true;
this.terminating = false;
if (this.ssl_enable) {
options = {
key: this.ssl_key ? fs.readFileSync(this.ssl_key) : null,
cert: this.ssl_cert ? fs.readFileSync(this.ssl_cert) : null,
passphrase: this.ssl_passphrase ? this.ssl_passphrase : null,
rejectUnauthorized: this.rejectUnauthorized === true,
ca: this.ca ? (function (caList) {
var caFilesList = [];
caList.forEach(function (filePath) {
caFilesList.push(fs.readFileSync(filePath));
});
return caFilesList;
}(this.ca)) : null
};
this.socket = new tls.connect(this.port, this.host, options, function() {
self.socket.setEncoding('UTF-8');
self.announce();
self.connecting = false;
});
} else {
this.socket = new net.Socket();
}
this.socket.on('error', function (err) {
self.connecting = false;
self.connected = false;
if (typeof(self.socket) !== 'undefined' && self.socket != null) {
self.socket.destroy();
}
self.socket = null;
if (!ECONNREFUSED_REGEXP.test(err.message)) {
tryReconnect = false;
self.emit('error', err);
}
});
this.socket.on('timeout', function() {
if (self.socket.readyState !== 'open') {
self.socket.destroy();
}
});
this.socket.on('connect', function () {
self.retries = 0;
});
this.socket.on('close', function (had_error) {
self.connected = false;
if (self.terminating) {
return;
}
if (self.max_connect_retries < 0 || self.retries < self.max_connect_retries) {
if (!self.connecting) {
setTimeout(function () {
self.connect();
}, self.timeout_connect_retries);
}
} else {
self.log_queue = [];
self.silent = true;
self.emit('error', new Error('Max retries reached, transport in silent mode, OFFLINE'));
}
});
if (!this.ssl_enable) {
this.socket.connect(self.port, self.host, function () {
self.announce();
self.connecting = false;
self.socket.setKeepAlive(true, 60 * 1000);
});
}
};
Logstash.prototype.close = function () {
var self = this;
self.terminating = true;
if (self.connected && self.socket) {
self.connected = false;
self.socket.end();
self.socket.destroy();
self.socket = null;
}
};
Logstash.prototype.announce = function () {
var self = this;
self.connected = true;
self.flush();
if (self.terminating) {
self.close();
}
};
Logstash.prototype.flush = function () {
var self = this;
for (var i = 0; i < self.log_queue.length; i++) {
self.sendLog(self.log_queue[i].message, self.log_queue[i].callback);
}
self.log_queue.length = 0;
};
Logstash.prototype.sendLog = function (message, callback) {
var self = this;
callback = callback || function () {};
self.socket.write(message + '\n');
callback();
};
Logstash.prototype.getQueueLength = function () {
return this.log_queue.length;
};