diff --git a/index.js b/index.js index 1491b5cfac08b15e2e43d70d0fcb55fbb2661c78..90e2a5a9ab39b9f40a4ef55a5ab14faecfdd3301 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const udp = require('dgram') const buffer = require('buffer') +const graylog = udp.createSocket('udp4') function Log (options) { this.levels = { @@ -46,10 +47,6 @@ function Log (options) { } } } - if (this.options.graylog && this.options.graylog.active) { - this.graylog = udp.createSocket('udp4') - } - this.info = function (message) { if (this.loglevel >= this.levels.INFO) { return this.log('INFO', message) @@ -124,7 +121,13 @@ function Log (options) { }) } if (this.options.graylog && this.options.graylog.active) { - this.graylogSend(message, tag) + graylog.send(Buffer.from(JSON.stringify({ + short_message: message, + timestamp: Date.now() / 1000, + hostname: this.hostname, + facility: this.name, + level:tag + })), this.options.graylog.port, this.options.graylog.server, function(error){}) } return msg } @@ -133,15 +136,6 @@ function Log (options) { var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1) return localISOTime.split('.')[0].trim() } - this.graylogSend = function (message, level) { - this.graylog.send(Buffer.from(JSON.stringify({ - short_message: message, - timestamp: Date.now() / 1000, - hostname: this.hostname, - facility: this.name, - level:level - })),this.options.graylog.port,this.options.graylog.server,function(error){}) - } return this } module.exports = Log