Last active
April 12, 2018 20:19
-
-
Save marcelog/dafb53e9cf319b16d932 to your computer and use it in GitHub Desktop.
quick UDP Logstash logging from nodejs
This file contains hidden or 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
var util = require('util'); | |
var dgram = require('dgram'); | |
function logstashUDP(host, port, type, message, fields, callback) { | |
var client = dgram.createSocket('udp4'); | |
var logObject = { | |
'@timestamp': (new Date).toISOString(), | |
type: type, | |
message: message, | |
fields: fields | |
}; | |
var m = new Buffer(JSON.stringify(logObject)); | |
client.send(m, 0, m.length, port, host, function(err, bytes) { | |
client.close(); | |
callback(err); | |
}); | |
} | |
logstashUDP('127.0.0.1', 10001, 'js', 'hello world', {'level': 'debug'}, function(err) { | |
if(err) { | |
console.log('Error logging: %s', util.inspect(err)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do we specify an index?