advanced-logger
Version:
Advanced logger module extendable with plugins. Works in nodejs and browsers
2 lines • 11.8 kB
JavaScript
module.exports=function(t){var e={};function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(o,r,function(e){return t[e]}.bind(null,r));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=4)}([function(t,e){t.exports=require("events")},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(10),r=n(11),i=n(3),s=function(){function t(t){this.serviceConfig=t.serviceConfig,this.defaultLogConfig=t.defaultLogConfig||{},t.serializer&&(this.serializer=t.serializer)}return t.prototype.serializer=function(t){return i.default.tryJSONStringify(t)||o.default(t)},t.prototype.sendAllLogs=function(t){var e=this;return this.preparePayload(t).then(function(t){var n=e.getHeaders();return e.serviceConfig.retryAttempts&&e.serviceConfig.retryAttempts>0?r.default.postRequest(e.serviceConfig,n,t).catch(function(){return r.default.delayedRetry(e.serviceConfig.retryAttempts,e.serviceConfig.retryInterval,r.default.postRequest.bind(e,e.serviceConfig,n,t))}):r.default.postRequest(e.serviceConfig,n,t)})},t.prototype.preparePayload=function(t){var e=this,n=t.map(function(t){return e.serializer(Object.assign({},e.defaultLogConfig,t))});return Promise.resolve(n.join("\n"))},t.prototype.destroy=function(){this.serviceConfig=null,this.defaultLogConfig=null},t.prototype.getHeaders=function(){return{}},t}();e.default=s},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),function(t){t[t.RAPID_FIRE_GROUPING=0]="RAPID_FIRE_GROUPING"}(e.TransformationEnum||(e.TransformationEnum={}))},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});e.default={getLogIdByFields:function(t,e){return e.map(function(e){return e+"-"+t[e]}).join("-")},tryJSONStringify:function(t){try{return JSON.stringify(t)}catch(t){return""}}}},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(5);e.AdvancedLogger=o.default;var r=n(2);e.TransformationEnum=r.TransformationEnum;var i=n(8),s=n(9),u=n(13),a=n(14),l=n(15),f=n(16),c=n(17),d=n(19),p={InstantStrategy:l.default,OnBundleSizeStrategy:f.default,OnRequestStrategy:d.default,OnIntervalStrategy:c.default};e.strategy=p;var h={SumologicService:u.default,LogglyService:s.default,ConsoleService:i.default,ElasticsearchService:a.default};e.service=h},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(6),r=function(){function t(t){this.configuration=t,this.logStore=new o.default({transformations:this.configuration.transformations}),this.strategy=this.configuration.strategy,this.service=this.configuration.service,this.logStore.eventEmitter.on("add",this.onAdd.bind(this)),this.logStore.eventEmitter.on("clear",this.onClear.bind(this)),this.logStore.eventEmitter.on("error",this.onStoreError.bind(this)),this.strategy.eventEmitter.on("send",this.onSend.bind(this)),this.strategy.eventEmitter.on("error",this.onStrategyError.bind(this))}return t.prototype.log=function(t){this.logStore.add(t)},t.prototype.sendAllLogs=function(){this.strategy.sendAll()},t.prototype.destroy=function(){this.logStore.destroy(),this.logStore=null,this.strategy.destroy(),this.strategy=null,this.service.destroy(),this.service=null,this.configuration=null},t.prototype.onStoreError=function(t){console.error(t)},t.prototype.onAdd=function(t){this.strategy.onAdd(t)},t.prototype.onClear=function(t){this.strategy.onClear()},t.prototype.onStrategyError=function(t){console.error(t)},t.prototype.onSend=function(){if(this.logStore.size()>0){var t=this.logStore.getAll();this.logStore.clear(),this.service.sendAllLogs(t).catch(function(t){return console.error(t)})}},t}();e.default=r},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(7),r=n(0),i=n(2),s=n(3),u=function(){function t(t){this.groupLeftIndex=-1,this.logs=[],this.eventEmitter=new r.EventEmitter,this.config=t,this.initTransformations()}return t.prototype.add=function(t){if(this.groupableConfig){var e=s.default.getLogIdByFields(t,this.groupableConfig.groupIdentityFields);this.identityMap.has(e)||this.logs.push(t),this.onAddToGroup(t)}else this.logs.push(t);this.eventEmitter.emit("add",{logCount:this.size()})},t.prototype.clear=function(){this.logs.length=0,this.eventEmitter.emit("clear",null)},t.prototype.getAll=function(){return this.throttledOnGroupFinalize&&this.throttledOnGroupFinalize.flush(),this.logs.slice()},t.prototype.size=function(){return this.logs.length},t.prototype.destroy=function(){this.throttledOnGroupFinalize&&this.throttledOnGroupFinalize.cancel(),this.logs.length=0,this.eventEmitter.removeAllListeners(),this.eventEmitter=null},t.prototype.initTransformations=function(){if(this.config.transformations){var t=this.config.transformations.find(function(t){return t.type===i.TransformationEnum.RAPID_FIRE_GROUPING});t&&(this.groupableConfig=t.configuration,this.identityMap=new Map,this.throttledOnGroupFinalize=o(this.onGroupFinalize.bind(this),this.groupableConfig.interval,{trailing:!0,leading:!1}),this.eventEmitter.on("add",this.throttledOnGroupFinalize))}},t.prototype.onAddToGroup=function(t){var e=s.default.getLogIdByFields(t,this.groupableConfig.groupIdentityFields);if(this.identityMap.has(e)){var n=this.identityMap.get(e);this.identityMap.set(e,n+1)}else this.identityMap.set(e,1)},t.prototype.onGroupFinalize=function(){var t=this.logs.length;if(t>0)for(var e=-1!==this.groupLeftIndex?this.groupLeftIndex:0;e<t;e++){var n=this.logs[e],o=s.default.getLogIdByFields(n,this.groupableConfig.groupIdentityFields);n[this.groupableConfig.groupFieldName]=this.identityMap.has(o)?this.identityMap.get(o):1}this.groupLeftIndex=t>0?t-1:-1,this.identityMap.clear()},t}();e.default=u},function(t,e){t.exports=require("lodash/throttle")},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(){}return t.prototype.preparePayload=function(t){var e=this;return Promise.resolve(t.map(function(t){return e.serializer(t)}))},t.prototype.sendAllLogs=function(t){return console.log(t),Promise.resolve()},t.prototype.destroy=function(){},t.prototype.serializer=function(t){return t},t}();e.default=o},function(t,e,n){"use strict";var o,r=this&&this.__extends||(o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)},function(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)});Object.defineProperty(e,"__esModule",{value:!0});var i=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return r(e,t),e.prototype.getHeaders=function(){return{"Content-Type":"text/plain"}},e}(n(1).default);e.default=i},function(t,e){t.exports=require("fast-safe-stringify")},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(12),r={postRequest:function(t,e,n){return o(t.url,{method:t.method,body:n,headers:e})},delayedRetry:function(t,e,n){return void 0===e&&(e=0),new Promise(function(t,o){setTimeout(function(){return n().then(t).catch(o)},e)}).catch(function(o){return t>1?r.delayedRetry(t-1,e,n):Promise.reject(o)})}};e.default=r},function(t,e){t.exports=require("node-fetch")},function(t,e,n){"use strict";var o,r=this&&this.__extends||(o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)},function(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)});Object.defineProperty(e,"__esModule",{value:!0});var i=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return r(e,t),e.prototype.getHeaders=function(){var t=this.serviceConfig;return{"Content-Type":"application/json","X-Sumo-Category":t.sourceCategory,"X-Sumo-Host":t.host,"X-Sumo-Name":t.sourceName}},e}(n(1).default);e.default=i},function(t,e,n){"use strict";var o,r=this&&this.__extends||(o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)},function(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)});Object.defineProperty(e,"__esModule",{value:!0});var i=n(1),s=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return r(e,t),e.prototype.preparePayload=function(t){var e=this,n=[];return t.forEach(function(t){var o=Object.assign({},e.defaultLogConfig,t);n.push(e.serializer(e.getLogMetaConfig(o[e.serviceConfig.logMetaIndexField||"Index"]))),n.push(e.serializer(o))}),Promise.resolve(n.join("\n")+"\n")},e.prototype.getHeaders=function(){return{"Content-Type":"application/json"}},e.prototype.getLogMetaConfig=function(t){return{index:{_index:t||"index",_type:"_doc"}}},e}(i.default);e.default=s},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(0),r=function(){function t(){this.eventEmitter=new o.EventEmitter}return t.prototype.onAdd=function(t){this.eventEmitter.emit("send")},t.prototype.onClear=function(){},t.prototype.sendAll=function(t){},t.prototype.destroy=function(){this.eventEmitter.removeAllListeners(),this.eventEmitter=null},t}();e.default=r},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(0),r=function(){function t(t){this.MAX_BUNDLE_SIZE=100,this.eventEmitter=new o.EventEmitter,t.maxBundle&&(this.MAX_BUNDLE_SIZE=t.maxBundle)}return t.prototype.onAdd=function(t){t&&t.logCount>=this.MAX_BUNDLE_SIZE&&this.eventEmitter.emit("send")},t.prototype.onClear=function(){},t.prototype.sendAll=function(t){this.eventEmitter.emit("send")},t.prototype.destroy=function(){this.eventEmitter.removeAllListeners(),this.eventEmitter=null},t}();e.default=r},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(0),r=n(18),i=function(){function t(t){this.SEND_INTERVAL=15e3,this.eventEmitter=new o.EventEmitter,t.interval&&(this.SEND_INTERVAL=t.interval),this.debouncedSend=r(this.send.bind(this),this.SEND_INTERVAL)}return t.prototype.onAdd=function(t){t&&t.logCount>0&&this.debouncedSend()},t.prototype.onClear=function(){},t.prototype.sendAll=function(t){this.eventEmitter.emit("send")},t.prototype.destroy=function(){this.eventEmitter.removeAllListeners(),this.eventEmitter=null},t.prototype.send=function(){this.eventEmitter.emit("send")},t}();e.default=i},function(t,e){t.exports=require("lodash/debounce")},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var o=n(0),r=function(){function t(){this.eventEmitter=new o.EventEmitter}return t.prototype.onAdd=function(t){},t.prototype.onClear=function(){},t.prototype.sendAll=function(t){this.eventEmitter.emit("send")},t.prototype.destroy=function(){this.eventEmitter.removeAllListeners(),this.eventEmitter=null},t}();e.default=r}]);
//# sourceMappingURL=advanced-logger.node.min.js.map