-
Notifications
You must be signed in to change notification settings - Fork 7
/
gelf.js
187 lines (146 loc) · 3.65 KB
/
gelf.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
'use strict'
const deflate = require('zlib').deflate
const dgram = require('dgram')
const EventEmitter = require('events')
const os = require('os')
const async = require('async')
const defaults = {
graylogPort: 12201,
graylogHostname: '127.0.0.1',
connection: 'wan',
maxChunkSizeWan: 1420,
maxChunkSizeLan: 8154
}
const GELF_MAGIC_NO = [ 0x1e, 0x0f ]
class Gelf extends EventEmitter {
constructor (options) {
super(options)
this.config = options = Object.assign({}, defaults, options)
this.client = this.openSocket()
this.on('gelf.log', (json = {}, cb = () => {}) => {
const msg = this.sanitizeMsg(json)
setImmediate(() => {
this.sendMessage(msg, cb)
})
})
this.on('gelf.message', (msg, cb = () => {}) => {
this.sendMessage(msg, cb)
})
}
openSocket () {
const client = dgram.createSocket('udp4')
client.unref()
return client
}
closeSocket () {
this.client.close()
}
sendMessage (msg, cb) {
this.compress(msg, (_, buf) => {
const m = this.maybeChunkMessage(buf)
this.send(m, cb)
})
}
maybeChunkMessage (buf) {
const {
connection,
maxChunkSizeWan,
maxChunkSizeLan
} = this.config
const bufSize = buf.length
if (connection === 'wan' && bufSize > maxChunkSizeWan) {
const chunks = this.getChunks(buf, maxChunkSizeWan)
return this.createPackets(chunks)
}
if (connection === 'lan' && bufSize > maxChunkSizeLan) {
const chunks = this.getChunks(buf, maxChunkSizeLan)
return this.createPackets(chunks)
}
return buf
}
send (msg, cb = () => {}) {
const { graylogPort, graylogHostname } = this.config
const client = this.client
if (!Array.isArray(msg)) {
msg = [ msg ]
}
const tasks = msg.map((m) => {
const self = this
return function task (cb) {
client.send(m, 0, m.length, graylogPort, graylogHostname, (err) => {
if (err) {
return cb(err) && self.emitError(err)
}
return cb(null)
})
}
})
async.series(tasks, (err) => {
if (err) return cb(err) && this.emitError(err)
cb(null)
})
}
compress (msg, cb = () => {}) {
deflate(msg, (err, buf) => {
if (err) {
cb(err)
return this.emitError(err)
}
cb(null, buf)
})
}
getChunks (buf, chunkSize) {
const res = []
for (let index = 0; index < buf.length; index += chunkSize) {
res.push(
Array.prototype.slice.call(buf, index, index + chunkSize)
)
}
return res
}
createPackets (chunks) {
const res = []
const count = chunks.length
const msgId = Math.floor(
Math.random() * (99999999 - 10000000)
) + 10000000
chunks.forEach((chunk, index) => {
res[index] = Buffer.from(
GELF_MAGIC_NO.concat(msgId, index, count, chunk)
)
})
return res
}
emitError (err) {
this.emit('error', err)
}
sanitizeMsg (msg) {
let json = {}
json.short_message = msg.short_message
if (typeof msg === 'string') {
json.short_message = msg
} else {
json = Object.assign({}, json, msg)
}
if (msg._id) {
return this.emitError(new Error('_id is not allowed'))
}
if (!msg.version) {
json.version = '1.0'
}
if (!msg.host) {
json.host = os.hostname()
}
if (!msg.timestamp) {
json.timestamp = new Date().getTime() / 1000
}
if (!msg.facility) {
json.facility = 'node.js'
}
if (!json.short_message) {
json.short_message = 'Gelf Shortmessage'
}
return JSON.stringify(json)
}
}
module.exports = Gelf