public
Created

A simple Logstash UDP logger for Winston.

  • Download Gist
LogstashUDP.js
JavaScript
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
// Really simple Winston Logstash UDP Logger
 
var dgram = require('dgram'),
util = require('util'),
os = require('os'),
winston = require('winston');
 
var LogstashUDP = module.exports = function (options) {
winston.Transport.call(this, options);
options = options || {};
 
this.name = 'logstashUdp';
this.localhost = options.localhost || os.hostname();
this.host = options.host || '127.0.0.1';
this.port = options.port || 9999;
this.application = options.appName || process.title;
this.pid = options.pid || process.pid;
 
this.client = dgram.createSocket("udp4");
};
 
 
util.inherits(LogstashUDP, winston.Transport);
 
LogstashUDP.prototype.log = function (level, msg, meta, callback) {
var self = this,
meta = winston.clone(meta || {});
 
if (self.silent) {
return callback(null, true);
}
 
var logEntry = {
"@message": msg,
"@source_host": self.localhost,
"@timestamp": new Date().toISOString(),
"@fields": {
"level": level,
"pid": self.pid,
"application": self.application
}
};
 
for (prop in meta) {
if (meta.hasOwnProperty(prop)) logEntry["@fields"][prop] = meta[prop];
}
 
self.sendLog(JSON.stringify(logEntry), callback);
};
 
 
LogstashUDP.prototype.sendLog = function (message, callback) {
var self = this;
callback = callback || function () {};
var buf = new Buffer(message)
self.client.send(buf, 0, buf.length, self.port, self.host, callback);
};
udp.conf
1 2 3 4 5 6 7
input {
udp {
format => "json_event"
port => 9999
type=> "app"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.