databridge-logger
Version:
aggregate logs from all databridge clients
61 lines (47 loc) • 3.7 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 _fs = require('mz/fs');
var _fs2 = _interopRequireDefault(_fs);
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"); } }
/**
* A backend that writes the logs to a single file
*
* It does not support log rotation
*/
var FileBackend = function () {
/**
* Create a new FileBackend from the path of the file to write
*
* @param {string} filename the file where we should store the file
*/
function FileBackend(filename) {
_classCallCheck(this, FileBackend);
this.file = _fs2.default.createWriteStream(filename, {
flags: 'a+',
defaultEncoding: 'utf8',
autoClose: true
});
}
/**
* Store a message/packet using this backend
*
* @param {Array<object>} messages the messages to persist
* @return {Promise} a promise when the writing is done
*/
_createClass(FileBackend, [{
key: 'store',
value: function store(messages) {
var mergedMessages = messages.map(JSON.stringify).join('\n');
return this.file.write(mergedMessages);
}
}]);
return FileBackend;
}();
exports.default = FileBackend;