diff --git a/index.js b/index.js index 81052067db4a5e7c954fa031a5d58e3b1b5f06c0..2fb5b072251446a0b720b9cbff1938f60c705b05 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ const udp = require('dgram') -const Buffer = require('buffer') -const graylog = udp.createSocket('udp4') function Log (options) { this.levels = { @@ -36,7 +34,9 @@ function Log (options) { } catch (error) { throw new Error('Could not create path ' + this.options.path + '\n' + error.toString()) } - + if (this.options.graylog && this.options.graylog.active) { + this.udpclient = udp.createSocket('udp4') + } try { this.fs.accessSync(this.path.join(this.options.path, this.options.file), this.fs.constants.R_OK | this.fs.constants.W_OK) } catch (err) { @@ -128,15 +128,15 @@ function Log (options) { facility: this.name, level:tag })) - graylog.connect(this.options.graylog.port, this.options.graylog.server, (err) => { + this.udpclient.connect(this.options.graylog.port, this.options.graylog.server, (err) => { if (err) { console.error(err) } else { - graylog.send(buffed, (error) => { + this.udpclient.send(buffed, (error) => { if (error) { console.error(error) } - graylog.close(); + this.udpclient.close() }) } })