Skip to content

Commit

Permalink
Naive GELF ouput implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Paquet committed Oct 14, 2012
1 parent 2801330 commit 08aadc3
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/outputs/output_gelf.js
@@ -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();
}

0 comments on commit 08aadc3

Please sign in to comment.