databridge-logger
Version:
aggregate logs from all databridge clients
119 lines (95 loc) • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
* @author Guillaume Leclerc <guillaume.leclerc.work@gmail.com>
*
*/
var _uuid = require('uuid');
var _uuid2 = _interopRequireDefault(_uuid);
var _lodash = require('lodash');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* The max size of the logging buffer.
*
* If it reach that size it should be directly forwarded to the backends
*
* @constant
*/
var MAX_BUFFER_SIZE = 100;
/**
* Allow to log things to a given backend
*/
var Logger = function () {
/**
* Create a new looger
*
* @param {string} application the name of the application bridge
* @param {Array | object} backends list of backends to use
* @param {string} identity the identity of the person logging things
* @param {number} aggregationTime the time frame to aggregate all the logs in a single block
*/
function Logger(application, backends) {
var _this = this;
var identity = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _uuid2.default.v4();
var aggregationTime = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 500;
_classCallCheck(this, Logger);
this.buffer = [];
this.drain = function () {
// Store for each backend
_this.backends.forEach(function (backend) {
backend.store(_this.buffer);
});
_this.buffer = [];
};
this.application = application;
this.identity = identity;
this.backends = backends;
if (!Array.isArray(this.backends)) {
this.backends = [this.backends];
}
this.drainDebounced = (0, _lodash.debounce)(this.drain, aggregationTime, {
maxWait: this.aggregationTime
});
}
/**
* Send and entire block of messages at once
*
* Also empties the buffer
*
* @return {undefined} Nothing to return
*/
_createClass(Logger, [{
key: 'log',
/**
* Log an event and pass it to the backend
*
* @param {string} eventId the unique id for this kind of event
* @param {object} details more info about the event
* @param {string} level the level of the log entry
* @return {Promise} the promise when the data has been successfully sent to the backend
*/
value: function log(eventId) {
var details = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'info';
var packet = {
identity: this.identity,
application: this.application,
timestamp: Date.now(),
eventId: eventId,
details: details,
level: level
};
this.buffer.push(packet);
if (this.buffer.length > MAX_BUFFER_SIZE) {
this.drain();
} else {
this.drainDebounced();
}
}
}]);
return Logger;
}();
exports.default = Logger;