advanced-logger
Version:
Advanced logger module extendable with plugins. Works in nodejs and browsers
2 lines • 8.11 kB
JavaScript
(()=>{"use strict";var t={n:e=>{var i=e&&e.__esModule?()=>e.default:()=>e;return t.d(i,{a:i}),i},d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{AdvancedLogger:()=>l,TransformationEnum:()=>o,service:()=>E,strategy:()=>m});const i=require("events"),n=require("lodash/throttle");var r=t.n(n);const s={getLogIdByFields:(t,e)=>e.map((e=>`${e}-${t[e]}`)).join("-"),tryJSONStringify(t){try{return JSON.stringify(t)}catch(t){return""}}};var o;!function(t){t[t.RAPID_FIRE_GROUPING=0]="RAPID_FIRE_GROUPING"}(o||(o={}));class a{constructor(t){if(this.groupLeftIndex=-1,this.logs=[],this.eventEmitter=new i.EventEmitter,this.config=t,this.identityMap=new Map,this.config.transformations){const t=this.config.transformations.find((t=>t.type===o.RAPID_FIRE_GROUPING));t&&(this.groupableConfig=t.configuration,this.throttledOnGroupFinalize=r()(this.onGroupFinalize.bind(this),this.groupableConfig.interval,{trailing:!0,leading:!1}),this.eventEmitter.on("add",this.throttledOnGroupFinalize))}}add(t){if(this.groupableConfig){const e=s.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()})}clear(){this.logs.length=0,this.eventEmitter.emit("clear")}getAll(){return this.throttledOnGroupFinalize&&this.throttledOnGroupFinalize.flush(),this.logs.slice()}size(){return this.logs.length}destroy(){this.throttledOnGroupFinalize&&this.throttledOnGroupFinalize.cancel(),this.logs.splice(0),this.eventEmitter.removeAllListeners()}onAddToGroup(t){const e=s.getLogIdByFields(t,this.groupableConfig.groupIdentityFields);if(this.identityMap.has(e)){const t=this.identityMap.get(e);this.identityMap.set(e,(t||0)+1)}else this.identityMap.set(e,1)}onGroupFinalize(){const t=this.logs.length;if(t>0)for(let e=-1!==this.groupLeftIndex?this.groupLeftIndex:0;e<t;e++){const t=this.logs[e],i=s.getLogIdByFields(t,this.groupableConfig.groupIdentityFields);Object.assign(t,{[this.groupableConfig.groupFieldName]:this.identityMap.has(i)?this.identityMap.get(i):1})}this.groupLeftIndex=t>0?t-1:-1,this.identityMap.clear()}}class l{constructor(t){this.configuration=t,this.logStore=new a({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))}log(t){this.logStore.add(t)}sendAllLogs(){this.strategy.sendAll()}destroy(){this.logStore.destroy(),this.strategy.destroy(),this.service.destroy()}onStoreError(t){console.error(t)}onAdd(t){this.strategy.onAdd(t)}onClear(){this.strategy.onClear()}onStrategyError(t){console.error(t)}onSend(){return t=this,e=void 0,n=function*(){if(this.logStore.size()>0){const t=this.logStore.getAll();this.logStore.clear();try{yield this.service.sendAllLogs(t)}catch(t){console.error(t)}}},new((i=void 0)||(i=Promise))((function(r,s){function o(t){try{l(n.next(t))}catch(t){s(t)}}function a(t){try{l(n.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}l((n=n.apply(t,e||[])).next())}));var t,e,i,n}}const d=require("fast-safe-stringify");var h=t.n(d);const c=require("axios");var u=t.n(c);const g={request:(t,e,i)=>u().request({method:t.method,headers:e,url:t.url,data:i}),delayedRetry:(t,e,i)=>new Promise(((t,n)=>{setTimeout((()=>i().then(t).catch(n)),e)})).catch((n=>t>1?g.delayedRetry(t-1,e,i):Promise.reject(n)))},v=g;var f=function(t,e,i,n){return new(i||(i=Promise))((function(r,s){function o(t){try{l(n.next(t))}catch(t){s(t)}}function a(t){try{l(n.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}l((n=n.apply(t,e||[])).next())}))};class y{constructor(t){this.serviceConfig=Object.assign({},t.serviceConfig),this.defaultLogConfig=Object.assign({},t.defaultLogConfig),t.serializer&&(this.serializer=t.serializer)}serializer(t){return s.tryJSONStringify(t)||h()(t)}sendAllLogs(t){var e,i,n,r,s,o;return f(this,void 0,void 0,(function*(){const a=yield this.preparePayload(t),l=this.getHeaders();try{return yield v.request(this.serviceConfig,l,a)}catch(t){if((null!==(i=null===(e=this.serviceConfig)||void 0===e?void 0:e.retryAttempts)&&void 0!==i?i:0)>0)return v.delayedRetry(null!==(r=null===(n=this.serviceConfig)||void 0===n?void 0:n.retryAttempts)&&void 0!==r?r:0,null!==(o=null===(s=this.serviceConfig)||void 0===s?void 0:s.retryInterval)&&void 0!==o?o:0,v.request.bind(this,this.serviceConfig,l,a));throw t}}))}preparePayload(t){return f(this,void 0,void 0,(function*(){return t.map((t=>this.serializer(Object.assign(Object.assign({},this.defaultLogConfig),t)))).join("\n")}))}destroy(){}getHeaders(){return{}}}var p=function(t,e,i,n){return new(i||(i=Promise))((function(r,s){function o(t){try{l(n.next(t))}catch(t){s(t)}}function a(t){try{l(n.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}l((n=n.apply(t,e||[])).next())}))};const m={InstantStrategy:class{constructor(){this.eventEmitter=new i.EventEmitter}onAdd(t){this.eventEmitter.emit("send")}onClear(){}sendAll(){}destroy(){this.eventEmitter.removeAllListeners()}},OnBundleSizeStrategy:class{constructor(t){this.MAX_BUNDLE_SIZE=100,this.eventEmitter=new i.EventEmitter,t.maxBundle&&(this.MAX_BUNDLE_SIZE=t.maxBundle)}onAdd(t){t&&t.logCount>=this.MAX_BUNDLE_SIZE&&this.eventEmitter.emit("send")}onClear(){}sendAll(){this.eventEmitter.emit("send")}destroy(){this.eventEmitter.removeAllListeners()}},OnRequestStrategy:class{constructor(){this.eventEmitter=new i.EventEmitter}onAdd(t){}onClear(){}sendAll(){this.eventEmitter.emit("send")}destroy(){this.eventEmitter.removeAllListeners()}},OnIntervalStrategy:class{constructor(t){this.SEND_INTERVAL=15e3,this.eventEmitter=new i.EventEmitter,t.interval&&(this.SEND_INTERVAL=t.interval),this.intervalSend=r()(this.send.bind(this),this.SEND_INTERVAL,{leading:!1,trailing:!0})}onAdd(t){t&&t.logCount>0&&this.intervalSend()}onClear(){}sendAll(){this.eventEmitter.emit("send")}destroy(){this.eventEmitter.removeAllListeners()}send(){this.eventEmitter.emit("send")}}},E={BaseRemoteService:y,SumologicService:class extends y{getHeaders(){const t=this.serviceConfig;return{"Content-Type":"application/json","X-Sumo-Category":t.sourceCategory,"X-Sumo-Host":t.host,"X-Sumo-Name":t.sourceName}}},LogglyService:class extends y{getHeaders(){return{"Content-Type":"text/plain"}}},ConsoleService:class{preparePayload(t){return p(this,void 0,void 0,(function*(){return t.map((t=>this.serializer(t)))}))}sendAllLogs(t){return p(this,void 0,void 0,(function*(){console.log(t)}))}destroy(){}serializer(t){return t}},ElasticsearchService:class extends y{preparePayload(t){return e=this,i=void 0,r=function*(){const e=[];return t.forEach((t=>{const i=Object.assign(Object.assign({},this.defaultLogConfig),t);e.push(this.serializer(this.getLogMetaConfig(i[this.serviceConfig.logMetaIndexField||"Index"]))),e.push(this.serializer(i))})),`${e.join("\n")}\n`},new((n=void 0)||(n=Promise))((function(t,s){function o(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(e){var i;e.done?t(e.value):(i=e.value,i instanceof n?i:new n((function(t){t(i)}))).then(o,a)}l((r=r.apply(e,i||[])).next())}));var e,i,n,r}getHeaders(){return{"Content-Type":"application/json"}}getLogMetaConfig(t){return{index:{_index:t||"index",_type:"_doc"}}}}};module.exports.advancedLogger=e})();
//# sourceMappingURL=advanced-logger.node.min.js.map