fluent-logger
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
var log4jsSupport = require('../lib/log4js'); | ||
var EventTime = require('./event-time').EventTime; | ||
@@ -36,3 +37,5 @@ module.exports = { | ||
appender: log4jsSupport.appender | ||
appender: log4jsSupport.appender, | ||
EventTime: EventTime | ||
}; | ||
@@ -39,0 +42,0 @@ |
@@ -9,3 +9,7 @@ 'use strict'; | ||
var FluentLoggerError = require('./logger-error'); | ||
var EventTime = require('./event-time').EventTime; | ||
var codec = msgpack.createCodec(); | ||
codec.addExtPacker(0x00, EventTime, EventTime.pack); | ||
codec.addExtUnpacker(0x00, EventTime.unpack); | ||
@@ -57,3 +61,3 @@ function FluentSender(tag_prefix, options){ | ||
error = new FluentLoggerError.MissingTag('tag is missing', options); | ||
self._handleError(error, 'error', callback); | ||
self._handleEvent('error', error, callback); | ||
return; | ||
@@ -68,3 +72,3 @@ } | ||
error = new FluentLoggerError.DataTypeError('data must be an object', options); | ||
self._handleError(error, 'error', callback); | ||
self._handleEvent('error', error, callback); | ||
return; | ||
@@ -94,3 +98,3 @@ } | ||
if (err) { | ||
self._handleError(err, 'error', callback); | ||
self._handleEvent('error', err, callback); | ||
} else { | ||
@@ -127,3 +131,3 @@ callback && callback(); | ||
if (typeof time !== "number") { | ||
if (typeof time !== "number" && !(time instanceof EventTime)) { | ||
time = Math.floor((time ? time.getTime() : Date.now()) / this._timeResolution); | ||
@@ -141,3 +145,3 @@ } | ||
return { | ||
packet: msgpack.encode(packet), | ||
packet: msgpack.encode(packet, { codec: codec }), | ||
tag: tag, | ||
@@ -159,5 +163,8 @@ time: time, | ||
self._socket = null; | ||
self._handleError(err, 'error', null); | ||
self._handleEvent('error', err); | ||
} | ||
}); | ||
self._socket.on('connect', function() { | ||
self._handleEvent('connect'); | ||
}); | ||
self._socket.on('data', function(data) { | ||
@@ -199,3 +206,3 @@ self._data = data; | ||
if (self._data) { | ||
var response = msgpack.decode(self._data); | ||
var response = msgpack.decode(self._data, { codec: codec }); | ||
self._data = null; | ||
@@ -206,3 +213,3 @@ clearInterval(intervalId); | ||
{ ack: response.ack, chunk: item.options.chunk }); | ||
self._handleError(error, 'error', item.callback); | ||
self._handleEvent('error', error, item.callback); | ||
} | ||
@@ -213,3 +220,3 @@ } | ||
var error = new FluentLoggerError.ResponseTimeout('ack response timeout'); | ||
self._handleError(error, 'error', item.callback); | ||
self._handleEvent('error', error, item.callback); | ||
clearInterval(intervalId); | ||
@@ -230,11 +237,11 @@ }, self.ackResponseTimeout); | ||
FluentSender.prototype._handleError = function(error, signal, callback) { | ||
FluentSender.prototype._handleEvent = function _handleEvent(signal, data, callback) { | ||
var self = this; | ||
callback && callback(error); | ||
callback && callback(data); | ||
if (self._eventEmitter.listenerCount(signal) > 0) { | ||
self._eventEmitter.emit(signal, error); | ||
self._eventEmitter.emit(signal, data); | ||
} | ||
}; | ||
FluentSender.prototype._setupErrorHandler = function() { | ||
FluentSender.prototype._setupErrorHandler = function _setupErrorHandler() { | ||
var self = this; | ||
@@ -277,7 +284,9 @@ if (!self.reconnectInterval) { | ||
if (!dataArray.length) { | ||
return process.nextTick(callback); | ||
process.nextTick(callback); | ||
return; | ||
} | ||
self.emit(label, { message: dataString }, function(err) { | ||
if (err) { | ||
return self._handleError(err, 'error', callback); | ||
self._handleEvent('error', err, callback); | ||
return; | ||
} | ||
@@ -287,3 +296,3 @@ dataString = ''; | ||
}); | ||
} | ||
}; | ||
next(); | ||
@@ -290,0 +299,0 @@ }; |
{ | ||
"name": "fluent-logger", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -68,2 +68,34 @@ # fluent-logger for Node.js | ||
### EventTime support | ||
We can also specify [EventTime](https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format) as timestamp. | ||
```js | ||
var FluentLogger = require('fluent-logger'); | ||
var EventTime = FluentLogger.EventTime; | ||
var logger = FluentLogger.createFluentSender('tag_prefix', { | ||
var eventTime = new EventTime(1489547207, 745003500); // 2017-03-15 12:06:47 +0900 | ||
logger.emit('tag', { message: 'This is a message' }, eventTime); | ||
``` | ||
### Events | ||
* `connect` : Handle [net.Socket Event: connect](https://nodejs.org/api/net.html#net_event_connect) | ||
* `error` : Handle [net.Socket Event: error](https://nodejs.org/api/net.html#net_event_error_1) | ||
```js | ||
var logger = require('fluent-logger').createFluentSender('tag_prefix', { | ||
host: 'localhost', | ||
port: 24224, | ||
timeout: 3.0, | ||
reconnectInterval: 600000 // 10 minutes | ||
}); | ||
logger.on('error', function(error) { | ||
console.log(error); | ||
}); | ||
logger.on('connect', function() { | ||
console.log('connected!'); | ||
}); | ||
``` | ||
## Logging Library Support | ||
@@ -70,0 +102,0 @@ |
var expect = require('chai').expect; | ||
var sender = require('../lib/sender'); | ||
var EventTime = require('../lib/event-time').EventTime; | ||
var runServer = require('../lib/testHelper').runServer; | ||
@@ -7,3 +8,8 @@ var stream = require('stream'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var msgpack = require('msgpack-lite'); | ||
var codec = msgpack.createCodec(); | ||
codec.addExtPacker(0x00, EventTime, EventTime.pack); | ||
codec.addExtUnpacker(0x00, EventTime.unpack); | ||
describe("FluentSender", function(){ | ||
@@ -34,2 +40,18 @@ it('should send records', function(done){ | ||
it('should emit connect event', function(done) { | ||
runServer({}, function(server, finish) { | ||
var s = new sender.FluentSender('debug', {port: server.port}); | ||
var called = false; | ||
s.on('connect', function() { | ||
called = true; | ||
}); | ||
s.emit({message: "1st message"}, function() { | ||
finish(function(data) { | ||
expect(called).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should raise error when connection fails', function(done){ | ||
@@ -96,2 +118,17 @@ var s = new sender.FluentSender('debug', { | ||
it('should allow to emit with a EventTime', function(done) { | ||
runServer({}, function(server, finish) { | ||
var s = new sender.FluentSender('debug', {port: server.port}); | ||
var eventTime = EventTime.now(); | ||
s.emit('1st record', { message: '1st data' }, eventTime, function() { | ||
finish(function(data) { | ||
var decoded = EventTime.unpack(data[0].time.buffer); | ||
expect(JSON.stringify(decoded)).to.equal(JSON.stringify(eventTime)); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should resume the connection automatically and flush the queue', function(done){ | ||
@@ -98,0 +135,0 @@ var s = new sender.FluentSender('debug'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69531
18
1213
248