Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Thayer committed Sep 24, 2015
2 parents faee3d5 + 9aa2900 commit c365c1d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.10"
- "0.8"
before_install:
Expand Down
21 changes: 17 additions & 4 deletions lib/winston-logstash-udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ util.inherits(LogstashUDP, winston.Transport);
winston.transports.LogstashUDP = LogstashUDP;

LogstashUDP.prototype.connect = function() {
var self = this;
this.client = dgram.createSocket('udp4');

// Attach an error listener on the socket
// It can also avoid top level exceptions like UDP DNS errors thrown by the socket
this.client.on('error', function(err) {
// in node versions <= 0.12, the error event is emitted even when a callback is passed to send()
// we always pass a callback to send(), so it's safe to do nothing here
});
};

LogstashUDP.prototype.log = function(level, msg, meta, callback) {
Expand All @@ -72,9 +80,14 @@ LogstashUDP.prototype.log = function(level, msg, meta, callback) {
json: true
});

self.sendLog(logEntry, function() {
self.emit('logged', true);
callback(null, true);
self.sendLog(logEntry, function (err) {
if (err) {
self.emit('error', err);
} else {
self.emit('logged', true);
}

callback(err, !err);
});

};
Expand All @@ -88,7 +101,7 @@ LogstashUDP.prototype.sendLog = function(message, callback) {

var buf = new Buffer(message);

callback = (callback || function() {});
callback = (callback || function (err) { if (err) { self.emit('error', err); } });

self.client.send(buf, 0, buf.length, self.port, self.host, callback);
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-logstash-udp",
"version": "0.0.7",
"version": "0.1.0",
"description": "A Logstash UDP transport for winston",
"main": "./lib/winston-logstash-udp",
"homepage": "https://github.com/sazze/winston-logstash-udp",
Expand Down Expand Up @@ -58,6 +58,10 @@
{
"name": "Pierre Inglebert",
"email": "[email protected]"
},
{
"name": "Kevin Danielo",
"email": "[email protected]"
}
],
"maintainers": [
Expand Down
12 changes: 12 additions & 0 deletions test/winston-logstash-udp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ describe('winston-logstash-udp transport', function () {

});

describe('without logstash server', function () {
it('emit an error message if UDP DNS errors occur on the socket', function (done) {
var logger = createLogger(port, {host:'unresolvedhost'});

logger.transports.logstashUdp.on('error', function (err) {
expect(err).to.be.an.instanceof(Error);
done();
});

logger.log('info', 'hello world', {stream: 'sample'});
});
});
});

0 comments on commit c365c1d

Please sign in to comment.