forked from egorFiNE/node-graylog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraylog.js
196 lines (154 loc) · 6.57 KB
/
graylog.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var zlib = require('zlib'),
crypto = require('crypto'),
async = require('async'),
dgram = require('dgram');
var graylog = function graylog(config) {
var key;
this.config = config;
this.servers = config.servers;
this.hostname = config.hostname || require('os').hostname();
this.facility = config.facility || 'Node.js';
this._callCount = 0;
this._bufferSize = config.bufferSize || this.DEFAULT_BUFFERSIZE;
this._dataSize = this._bufferSize - 12;
};
graylog.prototype.DEFAULT_BUFFERSIZE = 8192;
graylog.prototype.level = {
EMERG: 0, // system is unusable
ALERT: 1, // action must be taken immediately
CRIT: 2, // critical conditions
ERR: 3, // error conditions
ERROR: 3, // because people WILL typo
WARNING: 4, // warning conditions
NOTICE: 5, // normal, but significant, condition
INFO: 6, // informational message
DEBUG: 7 // debug level message
};
graylog.prototype.getServer = function () {
return this.servers[this._callCount++ % this.servers.length];
};
graylog.prototype.emergency = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.EMERGENCY);
};
graylog.prototype.alert = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.ALERT);
};
graylog.prototype.critical = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.CRIT);
};
graylog.prototype.error = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.ERROR);
};
graylog.prototype.warning = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.WARNING);
};
graylog.prototype.warn = graylog.prototype.warning;
graylog.prototype.notice = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.NOTICE);
};
graylog.prototype.info = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.INFO);
};
graylog.prototype.log = graylog.prototype.info;
graylog.prototype.debug = function (short_message, full_message, additionalFields, timestamp) {
return this._log(short_message, full_message, additionalFields, timestamp, this.level.DEBUG);
};
graylog.prototype._log = function log(short_message, full_message, additionalFields, timestamp, level) {
var payload,
fileinfo,
that = this,
field = '',
message = {
version : '1.0',
timestamp : (timestamp || new Date()).getTime() / 1000,
host : this.hostname,
facility : this.facility,
level : level
};
if (typeof(short_message) !== 'object') {
// We normally set the data
message.short_message = short_message;
message.full_message = full_message || short_message;
}
else if (short_message.stack && short_message.message) {
// Short message is an Error message, we process accordingly
message.short_message = short_message.message;
message.full_message = short_message.stack;
// extract error file and line
fileinfo = message.stack.split('\n')[0];
fileinfo = fileinfo.substr(fileinfo.indexOf('('), fileinfo.indeOf(')'));
fileinfo = fileinfo.split(':');
message.file = fileinfo[0];
message.line = fileinfo[1];
additionalFields = full_message || additionalFields;
}
else {
message.full_message = message.short_message = JSON.stringify(short_message);
}
// We insert additional fields
for (field in additionalFields) {
message['_' + field] = additionalFields[field];
}
// https://github.com/Graylog2/graylog2-docs/wiki/GELF
if (message._id) {
message.__id = message._id;
delete message._id;
}
// Compression
payload = new Buffer(JSON.stringify(message));
zlib.deflate(payload, function (err, buffer) {
if (err) {
return;
}
var chunkCount = Math.ceil(buffer.length / that._bufferSize),
server = that.getServer(),
client = dgram.createSocket("udp4");
if (chunkCount > 128) {
return console.error('Cannot send messages bigger than 1022.5K, not sending');
}
// If it all fits, just send it
if (chunkCount === 1) {
return that.send(buffer, client, server, function () {
client.close();
});
}
// Generate a random id in buffer format
crypto.randomBytes(8, function (err, id) {
// To be tested: whats faster, sending as we go or prebuffering?
var chunk = new Buffer(that._bufferSize),
start = 0,
stop = 0,
chunkSequenceNumber = 0;
// Set up magic number (0 and 1) and chunk total count (11)
chunk[0] = 30;
chunk[1] = 15;
chunk[11] = chunkCount;
// set message id (2,9)
id.copy(chunk, 2, 0, 8);
async.whilst(
function () { return chunkSequenceNumber < chunkCount; },
function (cb) {
// Set chunk sequence number
chunk[10] = chunkSequenceNumber;
// Select data from full buffer
start = chunkSequenceNumber * that._dataSize;
stop = Math.min((chunkSequenceNumber + 1) * that._dataSize, buffer.length);
buffer.copy(chunk, 12, start, stop);
chunkSequenceNumber++;
// Send a chunk of 8192 bytes or less
that.send(chunk.slice(0, stop - start + 12), client, server, cb);
},
function () { client.close(); }
);
});
});
};
graylog.prototype.send = function (chunk, client, server, cb) {
client.send(chunk, 0, chunk.length, server.port, server.host, function (err, byteCount) {
if (err) {
console.log(err);
}
cb();
});
};
exports.graylog = graylog;