Skip to content
Snippets Groups Projects
Commit 9800a90e authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

udp-datagrams working locally. should woork

parent 550f1b01
No related branches found
No related tags found
1 merge request!1Update index.test.js, examples/all-options.js, examples/log-to-file.js,...
......@@ -39,7 +39,3 @@ Note: The options Part may be omitted, as all parts are optional, but using the
loglevel: 'Minimal LogLevel. Default: WARN'
}
```
## Graylog
To use graylog, your graylog-server needs to have an http-input setup.
const Log = require('../index.js')
let log = new Log({
name: 'Sample-Application',
hostname: 'test-server',
graylog: {
active: true,
server: 'localhost',
port: 9999
}
})
log.info('This is an Information')
log.notice('This is a Notice')
log.warn('This is a Warning')
log.error('This is an Error')
log.fatal('This is a Fatal Message')
log.debug('This is a Debug Message')
var udp = require('dgram')
var server = udp.createSocket('udp4')
// emits when any error occurs
server.on('error',function(error){
console.log('Error: ' + error)
server.close()
})
// emits on new datagram msg
server.on('message',function(msg,info){
console.log('Data received from client : ' + msg.toString())
console.log('Received %d bytes from %s:%d\n',msg.length, info.address, info.port)
})
//emits when socket is ready and listening for datagram msgs
server.on('listening',function(){
var address = server.address()
var port = address.port
var family = address.family
var ipaddr = address.address
console.log('Server is listening at port' + port)
console.log('Server ip :' + ipaddr)
console.log('Server is IP4/IP6 : ' + family)
});
//emits after the socket is closed using socket.close()
server.on('close',function(){
console.log('Socket is closed !')
})
server.bind(9999);
\ No newline at end of file
......@@ -25,7 +25,8 @@ function Log (options) {
this.hostname = os.hostname()
}
if (this.options.graylog && this.options.graylog.active) {
this.http = require('http')
this.udp = require('dgram')
this.udpclient = this.udp.createSocket('udp4')
}
if (this.options.file) {
this.fs = require('fs')
......@@ -126,21 +127,11 @@ function Log (options) {
facility: this.name,
level: tag
})
let options = {
hostname: this.options.graylog.server,
port: this.options.graylog.port,
path: '/gelf',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
this.udpclient.send(Buffer.from(data),this.options.graylog.port,this.options.graylog.server,function(error){
if(error){
console.error(error)
}
var post = this.http.request(options)
// post the data
post.write(data)
post.end()
})
}
return msg
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment