winston-aws-cloudwatch
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -19,6 +19,14 @@ 'use strict'; | ||
var _libMessage = require('./lib/Message'); | ||
var _libCloudWatchClient = require('./lib/CloudWatchClient'); | ||
var _libMessage2 = _interopRequireDefault(_libMessage); | ||
var _libCloudWatchClient2 = _interopRequireDefault(_libCloudWatchClient); | ||
var _libLogItem = require('./lib/LogItem'); | ||
var _libLogItem2 = _interopRequireDefault(_libLogItem); | ||
var _libRelay = require('./lib/Relay'); | ||
var _libRelay2 = _interopRequireDefault(_libRelay); | ||
var _libQueue = require('./lib/Queue'); | ||
@@ -28,17 +36,17 @@ | ||
var _libWorker = require('./lib/Worker'); | ||
var _libWorker2 = _interopRequireDefault(_libWorker); | ||
var CloudWatchTransport = (function (_Transport) { | ||
_inherits(CloudWatchTransport, _Transport); | ||
function CloudWatchTransport(options) { | ||
function CloudWatchTransport(_ref) { | ||
var logGroupName = _ref.logGroupName; | ||
var logStreamName = _ref.logStreamName; | ||
var awsConfig = _ref.awsConfig; | ||
_classCallCheck(this, CloudWatchTransport); | ||
_get(Object.getPrototypeOf(CloudWatchTransport.prototype), 'constructor', this).call(this); | ||
this._worker = new _libWorker2['default'](options.logGroupName, options.logStreamName, { | ||
awsConfig: options.awsConfig | ||
}); | ||
this._queue = new _libQueue2['default'](this._worker); | ||
this._queue = new _libQueue2['default'](); | ||
var client = new _libCloudWatchClient2['default'](logGroupName, logStreamName, { awsConfig: awsConfig }); | ||
var relay = new _libRelay2['default'](client); | ||
relay.start(this._queue); | ||
} | ||
@@ -49,3 +57,3 @@ | ||
value: function log(level, msg, meta, callback) { | ||
this._queue.push(new _libMessage2['default'](+new Date(), level, msg, meta)); | ||
this._queue.push(new _libLogItem2['default'](+new Date(), level, msg, meta)); | ||
callback(null, true); | ||
@@ -52,0 +60,0 @@ } |
'use strict'; | ||
var _get = require('babel-runtime/helpers/get')['default']; | ||
var _inherits = require('babel-runtime/helpers/inherits')['default']; | ||
var _createClass = require('babel-runtime/helpers/create-class')['default']; | ||
@@ -7,4 +11,2 @@ | ||
var _Promise = require('babel-runtime/core-js/promise')['default']; | ||
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default']; | ||
@@ -20,24 +22,15 @@ | ||
var _defaults = require('defaults'); | ||
var _events = require('events'); | ||
var _defaults2 = _interopRequireDefault(_defaults); | ||
var debug = (0, _debug3['default'])('winston-aws-cloudwatch/Queue'); | ||
var _util = require('./util'); | ||
var Queue = (function (_EventEmitter) { | ||
_inherits(Queue, _EventEmitter); | ||
var debug = (0, _debug3['default'])('winston-aws-cloudwatch/Queue'); | ||
var Queue = (function () { | ||
function Queue(worker, options) { | ||
function Queue() { | ||
_classCallCheck(this, Queue); | ||
debug('constructor', { worker: worker, options: options }); | ||
this._worker = worker; | ||
this._options = (0, _defaults2['default'])(options, { | ||
flushInterval: 2000, | ||
errorDelay: 1000, | ||
batchSize: 20 | ||
}); | ||
this._items = []; | ||
this._flushing = null; | ||
this._lastFlushStarted = 0; | ||
_get(Object.getPrototypeOf(Queue.prototype), 'constructor', this).call(this); | ||
this._contents = []; | ||
this._locked = false; | ||
} | ||
@@ -49,71 +42,55 @@ | ||
debug('push', { item: item }); | ||
this._items.push(item); | ||
this._delayedFlush(); | ||
this._contents.push(item); | ||
this.emit('push', item); | ||
} | ||
}, { | ||
key: '_delayedFlush', | ||
value: function _delayedFlush() { | ||
var _this = this; | ||
if (this._flushing) { | ||
debug('delayedFlush: already flushing'); | ||
return; | ||
key: 'head', | ||
value: function head(num) { | ||
debug('head', { num: num }); | ||
if (this._locked) { | ||
throw new Error('Not allowed'); | ||
} | ||
var remainingTime = this._computeRemainingTime(); | ||
debug('delayedFlush: next flush in ' + remainingTime + ' ms'); | ||
this._flushing = (0, _util.delay)(remainingTime).then(function () { | ||
return _this._flush(); | ||
}).then(function () { | ||
return _this._flushing = null; | ||
}); | ||
return this._contents.slice(0, num); | ||
} | ||
}, { | ||
key: '_flush', | ||
value: function _flush() { | ||
var _this2 = this; | ||
if (this._items.length === 0) { | ||
debug('flush: queue empty'); | ||
return _Promise.resolve(); | ||
key: 'remove', | ||
value: function remove(num) { | ||
debug('remove', { num: num }); | ||
if (this._locked) { | ||
throw new Error('Not allowed'); | ||
} | ||
this._lastFlushStarted = +new Date(); | ||
var batch = this._items.slice(0, this._options.batchSize); | ||
debug('flush: flushing ' + batch.length); | ||
return this._worker.process(batch).then(function () { | ||
return _this2._onProcessed(); | ||
}, function (err) { | ||
return _this2._onError(err); | ||
}); | ||
this._contents.splice(0, num); | ||
} | ||
}, { | ||
key: '_onProcessed', | ||
value: function _onProcessed() { | ||
debug('onProcessed'); | ||
this._items.splice(0, this._options.batchSize); | ||
return this._flush(); | ||
key: 'lock', | ||
value: function lock() { | ||
if (this._locked) { | ||
throw new Error('Already locked'); | ||
} | ||
this._locked = true; | ||
} | ||
}, { | ||
key: '_onError', | ||
value: function _onError(err) { | ||
var _this3 = this; | ||
debug('onError', { error: err }); | ||
console.warn('Error: %s', err.stack || err); | ||
return (0, _util.delay)(this._options.errorDelay).then(function () { | ||
return _this3._flush(); | ||
}); | ||
key: 'unlock', | ||
value: function unlock() { | ||
if (!this._locked) { | ||
throw new Error('Not locked'); | ||
} | ||
this._locked = false; | ||
} | ||
}, { | ||
key: '_computeRemainingTime', | ||
value: function _computeRemainingTime() { | ||
var nextFlush = this._lastFlushStarted + this._options.flushInterval; | ||
var now = +new Date(); | ||
return Math.max(0, Math.min(this._options.flushInterval, nextFlush - now)); | ||
key: 'size', | ||
get: function get() { | ||
return this._contents.length; | ||
} | ||
}, { | ||
key: 'locked', | ||
get: function get() { | ||
return this._locked; | ||
} | ||
}]); | ||
return Queue; | ||
})(); | ||
})(_events.EventEmitter); | ||
exports['default'] = Queue; | ||
module.exports = exports['default']; |
@@ -16,4 +16,8 @@ 'use strict'; | ||
var find = function find(arr, fn) { | ||
return arr.filter(fn).shift(); | ||
}; | ||
var isEmpty = function isEmpty(obj) { | ||
return obj != null && _Object$keys(obj).length > 0; | ||
return obj == null || _Object$keys(obj).length === 0; | ||
}; | ||
@@ -23,4 +27,5 @@ | ||
delay: delay, | ||
find: find, | ||
isEmpty: isEmpty | ||
}; | ||
module.exports = exports['default']; |
{ | ||
"name": "winston-aws-cloudwatch", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A Winston transport for Amazon CloudWatch.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
22727
11
560