Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for close() method to allow for clean shutdown #27

Merged
merged 3 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/winston-logstash.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Logstash.prototype.connect = function () {
var self = this;
this.retries++;
this.connecting = true;
this.terminating = false;
if (this.ssl_enable) {
options = {
key: this.ssl_key ? fs.readFileSync(this.ssl_key) : null,
Expand Down Expand Up @@ -203,10 +204,24 @@ Logstash.prototype.connect = function () {

};

Logstash.prototype.close = function () {
var self = this;
self.terminating = true;
if (self.connected && self.socket) {
self.connected = false;
self.socket.end();
self.socket.destroy();
self.socket = null;
}
};

Logstash.prototype.announce = function () {
var self = this;
self.connected = true;
self.flush();
if (self.terminating) {
self.close();
}
};

Logstash.prototype.flush = function () {
Expand Down
55 changes: 28 additions & 27 deletions test/winston-logstash_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('winston-logstash transport', function() {
}

describe('with logstash server', function () {
var test_server, port = 28777;
var test_server, logger, port = 28777;

beforeEach(function(done) {
timekeeper.freeze(freezed_time);
Expand All @@ -88,8 +88,8 @@ describe('winston-logstash transport', function() {

it('send logs over TCP as valid json', function(done) {
var response;
var logger = createLogger(port);
var expected = {"stream":"sample","level":"info","message":"hello world"};
var expected = {"stream":"sample","level":"info","message":"hello world","label":"test"};
logger = createLogger(port);

test_server = createTestServer(port, function (data) {
response = data.toString();
Expand All @@ -102,11 +102,11 @@ describe('winston-logstash transport', function() {

it('send each log with a new line character', function(done) {
var response;
var logger = createLogger(port);
logger = createLogger(port);

test_server = createTestServer(port, function (data) {
response = data.toString();
expect(response).to.be.equal('{"stream":"sample","level":"info","message":"hello world"}\n');
expect(response).to.be.equal('{"stream":"sample","level":"info","message":"hello world","label":"test"}\n');
done();
});

Expand All @@ -116,11 +116,11 @@ describe('winston-logstash transport', function() {
it('send with different log levels', function(done) {

var response;
var logger = createLogger(port);
logger = createLogger(port);

test_server = createTestServer(port, function (data) {
response = data.toString();
expect(response).to.be.equal('{"stream":"sample","level":"info","message":"hello world"}\n{"stream":"sample","level":"error","message":"hello world"}\n');
expect(response).to.be.equal('{"stream":"sample","level":"info","message":"hello world","label":"test"}\n{"stream":"sample","level":"error","message":"hello world","label":"test"}\n');
done();
});

Expand All @@ -131,11 +131,11 @@ describe('winston-logstash transport', function() {

it('send with overrided meta data', function(done) {
var response;
var logger = createLogger(port, false, '', { meta: { default_meta_override: 'foo' } });
logger = createLogger(port, false, '', { meta: { default_meta_override: 'foo' } });
test_server = createTestServer(port, function (data) {
response = data.toString();

expect(response).to.be.equal('{"default_meta_override":"foo","level":"info","message":"hello world"}\n');
expect(response).to.be.equal('{"default_meta_override":"foo","level":"info","message":"hello world","label":"test"}\n');
done();
});

Expand All @@ -144,17 +144,21 @@ describe('winston-logstash transport', function() {

// Teardown
afterEach(function () {
if (logger) {
logger.close();
}
if (test_server) {
test_server.close(function () {});
}
timekeeper.reset();
test_server = null;
logger = null;
});

});

describe('with secured logstash server', function() {
var test_server, port = 28777;
var test_server, logger, port = 28777;

beforeEach(function(done) {
timekeeper.freeze(freezed_time);
Expand All @@ -163,8 +167,8 @@ describe('winston-logstash transport', function() {

it('send logs over SSL secured TCP as valid json', function(done) {
var response;
var logger = createLogger(port, true, __dirname + '/support/ssl/server.cert');
var expected = {"stream":"sample","level":"info","message":"hello world"};
var expected = {"stream":"sample","level":"info","message":"hello world","label":"test"};
logger = createLogger(port, true, __dirname + '/support/ssl/server.cert');

test_server = createTestSecureServer(port, {}, function (data) {
response = data.toString();
Expand All @@ -177,8 +181,8 @@ describe('winston-logstash transport', function() {

it('send logs over SSL secured TCP as valid json with SSL verification', function(done) {
var response;
var logger = createLogger(port, true, __dirname + '/support/ssl/server.cert');
var expected = {"stream":"sample","level":"info","message":"hello world"};
var expected = {"stream":"sample","level":"info","message":"hello world","label":"test"};
logger = createLogger(port, true, __dirname + '/support/ssl/server.cert');

test_server = createTestSecureServer(port, { verify: true }, function (data) {
response = data.toString();
Expand All @@ -192,9 +196,9 @@ describe('winston-logstash transport', function() {

it('logstash transport receive an error when there is a connection error different from ECONNREFUSED', function(done) {
var response,
logger = createLogger(port, true, __dirname + '/support/ssl/server-fail.cert'),
expected = {"stream":"sample","level":"info","message":"hello world"},
expected = {"stream":"sample","level":"info","message":"hello world","label":"test"},
silence = true;
logger = createLogger(port, true, __dirname + '/support/ssl/server-fail.cert'),

test_server = createTestSecureServer(port, {
serverKey: __dirname + '/support/ssl/server-fail.key',
Expand All @@ -203,7 +207,10 @@ describe('winston-logstash transport', function() {
}, function (data) {
response = data.toString();
expect(JSON.parse(response)).to.be.eql(expected);
done();
if (silence) {
done();
silence = false;
}
});

logger.transports.logstash.on('error', function (err) {
Expand All @@ -219,6 +226,9 @@ describe('winston-logstash transport', function() {

// Teardown
afterEach(function () {
if (logger) {
logger.close();
}
if (test_server) {
test_server.close(function () {});
}
Expand All @@ -231,20 +241,11 @@ describe('winston-logstash transport', function() {
describe('without logstash server', function () {
var logger, interval;

var checkSocketStatus = function (retries, logger, done) {
interval = setInterval(function() {
if (logger.transports.logstash.retries == retries) {
clearInterval(interval);
done();
}
}, 500);
};

it('fallback to silent mode if logstash server is down', function(done) {
var response;
var logger = createLogger(28747);

checkSocketStatus (4, logger, function() {
logger.transports.logstash.on('error', function (err) {
expect(logger.transports.logstash.silent).to.be.true;
done();
});
Expand Down