-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bertrand Paquet
committed
Oct 14, 2012
1 parent
2801330
commit 08aadc3
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
var base_output = require('../lib/base_output'), | ||
util = require('util'), | ||
dgram = require('dgram'), | ||
logger = require('log4node'), | ||
zlib = require('zlib'), | ||
error_buffer = require('../lib/error_buffer'); | ||
|
||
function OutputGelf() { | ||
base_output.BaseOutput.call(this); | ||
this.config = { | ||
name: 'Udp', | ||
host_field: 'host', | ||
port_field: 'port', | ||
optional_params: ['error_buffer_delay', 'version', 'message', 'facility', 'level'], | ||
default_values: { | ||
'error_buffer_delay': 2000, | ||
'version': '1.0', | ||
'message': '#{@message}', | ||
'facility': 'Node-logstash GELF', | ||
'level': '6', | ||
} | ||
} | ||
} | ||
|
||
util.inherits(OutputGelf, base_output.BaseOutput); | ||
|
||
OutputGelf.prototype.afterLoadConfig = function(callback) { | ||
logger.info('Start GELF output to', this.host + ':' + this.port); | ||
|
||
this.socket = dgram.createSocket('udp4'); | ||
|
||
this.error_buffer = error_buffer.create('output GELF to ' + this.host + ':' + this.port, this.error_buffer_delay, this); | ||
|
||
callback(); | ||
} | ||
|
||
OutputGelf.prototype.process = function(data) { | ||
var m = { | ||
version: this.version, | ||
short_message: this.replaceByFields(data, this.message), | ||
facility: this.replaceByFields(data, this.facility), | ||
host: data['@source_host'], | ||
level: this.replaceByFields(data, this.level), | ||
timestamp: (new Date(data['@timestamp'])).getTime() / 1000 >> 0, | ||
}; | ||
if (!m.short_message) { | ||
return; | ||
} | ||
logger.debug('Sending GELF', m); | ||
zlib.deflate(new Buffer(JSON.stringify(m)), function(err, message) { | ||
if (err) { | ||
return this.error_buffer.emit('error', new Error('Error while compressing data:' + err)); | ||
} | ||
this.socket.send(message, 0, message.length, this.port, this.host, function(err, bytes) { | ||
if (err || bytes != message.length) { | ||
this.error_buffer.emit('error', new Error('Error while send data to ' + this.host + ':' + this.port + ':' + err)); | ||
} | ||
}.bind(this)); | ||
}.bind(this)); | ||
} | ||
|
||
OutputGelf.prototype.close = function(callback) { | ||
logger.info('Closing GELF output to udp', this.host + ':' + this.port); | ||
this.socket.close(); | ||
callback(); | ||
} | ||
|
||
exports.create = function() { | ||
return new OutputGelf(); | ||
} |