fluent-logger
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -21,6 +21,16 @@ var util = require('util'); | ||
FluentSender.prototype.emit = function(label, data, callback){ | ||
FluentSender.prototype.emit = function(label, data /*, timestamp, callback */){ | ||
var timestamp, callback; | ||
if (arguments.length == 3 && typeof arguments[2] == 'function') { | ||
callback = arguments[2]; | ||
} else { | ||
timestamp = arguments[2]; | ||
callback = arguments[3]; | ||
} | ||
var self = this; | ||
var item = self._makePacketItem(label, data); | ||
var item = self._makePacketItem(label, data, timestamp); | ||
item.callback = callback; | ||
self._sendQueue.push(item); | ||
@@ -62,6 +72,11 @@ self._sendQueueTail++; | ||
FluentSender.prototype._makePacketItem = function(label, data, callback){ | ||
FluentSender.prototype._makePacketItem = function(label, data, time){ | ||
var self = this; | ||
var tag = [self.tag, label].join('.'); | ||
var time = (new Date()).getTime() / this._timeResolution; | ||
if (typeof time != "number") { | ||
var dateTime = time || new Date(); | ||
time = dateTime.getTime() / this._timeResolution; | ||
} | ||
var packet = [tag, time, data]; | ||
@@ -68,0 +83,0 @@ return { |
@@ -46,7 +46,19 @@ var net = require('net'); | ||
var self = this; | ||
this._server.close(function(){ | ||
callback(); | ||
}); | ||
if( process.version.match(/^v0\.6\./) ){ // 0.6.x does not support callback for server.close(); | ||
this._server.close(); | ||
(function waitForClose(){ | ||
if( Object.keys(self._clients).length > 0 ){ | ||
setTimeout(waitForClose, 100); | ||
}else{ | ||
callback(); | ||
} | ||
})(); | ||
}else{ | ||
this._server.close(function(){ | ||
callback(); | ||
}); | ||
} | ||
for(var i in self._clients){ | ||
self._clients[i].destroy(); | ||
self._clients[i].end(); | ||
// self._clients[i].destroy(); | ||
} | ||
@@ -53,0 +65,0 @@ }; |
{ | ||
"name": "fluent-logger", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -63,2 +63,30 @@ var expect = require('chai').expect; | ||
it('should allow to emit with a custom timestamp', function(done){ | ||
runServer(function(server, finish){ | ||
var s = new sender.FluentSender('debug', {port: server.port}); | ||
var timestamp = new Date(); | ||
s.emit("1st record", "1st data", timestamp, function() { | ||
finish(function(data) { | ||
expect(data[0].time).to.be.equal(timestamp.getTime() / 1000); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should allow to emit with a custom numeric timestamp', function(done){ | ||
runServer(function(server, finish){ | ||
var s = new sender.FluentSender('debug', {port: server.port}); | ||
var timestamp = new Date().getTime() / 1000; | ||
s.emit("1st record", "1st data", timestamp, function() { | ||
finish(function(data) { | ||
expect(data[0].time).to.be.equal(timestamp); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should resume the connection automatically and flush the queue', function(done){ | ||
@@ -65,0 +93,0 @@ var s = new sender.FluentSender('debug'); |
15573
439