UNPKG

loggish

Version:

Universal logger is a library designed for collect and process logs in apps

2 lines (1 loc) 8.71 kB
var Y=Object.defineProperty;var $=(s,f,c)=>f in s?Y(s,f,{enumerable:!0,configurable:!0,writable:!0,value:c}):s[f]=c;var S=(s,f,c)=>($(s,typeof f!="symbol"?f+"":f,c),c),P=(s,f,c)=>{if(!f.has(s))throw TypeError("Cannot "+c)};var l=(s,f,c)=>(P(s,f,"read from private field"),c?c.call(s):f.get(s)),y=(s,f,c)=>{if(f.has(s))throw TypeError("Cannot add the same private member more than once");f instanceof WeakSet?f.add(s):f.set(s,c)},N=(s,f,c,m)=>(P(s,f,"write to private field"),m?m.call(s,c):f.set(s,c),c);(function(s,f){typeof exports=="object"&&typeof module<"u"?module.exports=f():typeof define=="function"&&define.amd?define(f):(s=typeof globalThis<"u"?globalThis:s||self,s.UniversalLogger=f())})(this,function(){var h,L,b,v;"use strict";var s=(n=>(n.EMERGENCY="emergency",n.ERROR="error",n.CRITICAL="critical",n.WARN="warn",n.ALERT="alert",n.NOTICE="notice",n.INFO="info",n.DEBUG="debug",n))(s||{}),f={exports:{}},c=typeof Reflect=="object"?Reflect:null,m=c&&typeof c.apply=="function"?c.apply:function(e,t,r){return Function.prototype.apply.call(e,t,r)},g;c&&typeof c.ownKeys=="function"?g=c.ownKeys:Object.getOwnPropertySymbols?g=function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:g=function(e){return Object.getOwnPropertyNames(e)};function W(n){console&&console.warn&&console.warn(n)}var O=Number.isNaN||function(e){return e!==e};function u(){u.init.call(this)}f.exports=u,f.exports.once=K,u.EventEmitter=u,u.prototype._events=void 0,u.prototype._eventsCount=0,u.prototype._maxListeners=void 0;var _=10;function R(n){if(typeof n!="function")throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof n)}Object.defineProperty(u,"defaultMaxListeners",{enumerable:!0,get:function(){return _},set:function(n){if(typeof n!="number"||n<0||O(n))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+n+".");_=n}}),u.init=function(){(this._events===void 0||this._events===Object.getPrototypeOf(this)._events)&&(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},u.prototype.setMaxListeners=function(e){if(typeof e!="number"||e<0||O(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this};function x(n){return n._maxListeners===void 0?u.defaultMaxListeners:n._maxListeners}u.prototype.getMaxListeners=function(){return x(this)},u.prototype.emit=function(e){for(var t=[],r=1;r<arguments.length;r++)t.push(arguments[r]);var i=e==="error",a=this._events;if(a!==void 0)i=i&&a.error===void 0;else if(!i)return!1;if(i){var o;if(t.length>0&&(o=t[0]),o instanceof Error)throw o;var d=new Error("Unhandled error."+(o?" ("+o.message+")":""));throw d.context=o,d}var E=a[e];if(E===void 0)return!1;if(typeof E=="function")m(E,this,t);else for(var M=E.length,B=I(E,M),r=0;r<M;++r)m(B[r],this,t);return!0};function C(n,e,t,r){var i,a,o;if(R(t),a=n._events,a===void 0?(a=n._events=Object.create(null),n._eventsCount=0):(a.newListener!==void 0&&(n.emit("newListener",e,t.listener?t.listener:t),a=n._events),o=a[e]),o===void 0)o=a[e]=t,++n._eventsCount;else if(typeof o=="function"?o=a[e]=r?[t,o]:[o,t]:r?o.unshift(t):o.push(t),i=x(n),i>0&&o.length>i&&!o.warned){o.warned=!0;var d=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+String(e)+" listeners added. Use emitter.setMaxListeners() to increase limit");d.name="MaxListenersExceededWarning",d.emitter=n,d.type=e,d.count=o.length,W(d)}return n}u.prototype.addListener=function(e,t){return C(this,e,t,!1)},u.prototype.on=u.prototype.addListener,u.prototype.prependListener=function(e,t){return C(this,e,t,!0)};function D(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length===0?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function T(n,e,t){var r={fired:!1,wrapFn:void 0,target:n,type:e,listener:t},i=D.bind(r);return i.listener=t,r.wrapFn=i,i}u.prototype.once=function(e,t){return R(t),this.on(e,T(this,e,t)),this},u.prototype.prependOnceListener=function(e,t){return R(t),this.prependListener(e,T(this,e,t)),this},u.prototype.removeListener=function(e,t){var r,i,a,o,d;if(R(t),i=this._events,i===void 0)return this;if(r=i[e],r===void 0)return this;if(r===t||r.listener===t)--this._eventsCount===0?this._events=Object.create(null):(delete i[e],i.removeListener&&this.emit("removeListener",e,r.listener||t));else if(typeof r!="function"){for(a=-1,o=r.length-1;o>=0;o--)if(r[o]===t||r[o].listener===t){d=r[o].listener,a=o;break}if(a<0)return this;a===0?r.shift():G(r,a),r.length===1&&(i[e]=r[0]),i.removeListener!==void 0&&this.emit("removeListener",e,d||t)}return this},u.prototype.off=u.prototype.removeListener,u.prototype.removeAllListeners=function(e){var t,r,i;if(r=this._events,r===void 0)return this;if(r.removeListener===void 0)return arguments.length===0?(this._events=Object.create(null),this._eventsCount=0):r[e]!==void 0&&(--this._eventsCount===0?this._events=Object.create(null):delete r[e]),this;if(arguments.length===0){var a=Object.keys(r),o;for(i=0;i<a.length;++i)o=a[i],o!=="removeListener"&&this.removeAllListeners(o);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if(t=r[e],typeof t=="function")this.removeListener(e,t);else if(t!==void 0)for(i=t.length-1;i>=0;i--)this.removeListener(e,t[i]);return this};function A(n,e,t){var r=n._events;if(r===void 0)return[];var i=r[e];return i===void 0?[]:typeof i=="function"?t?[i.listener||i]:[i]:t?U(i):I(i,i.length)}u.prototype.listeners=function(e){return A(this,e,!0)},u.prototype.rawListeners=function(e){return A(this,e,!1)},u.listenerCount=function(n,e){return typeof n.listenerCount=="function"?n.listenerCount(e):F.call(n,e)},u.prototype.listenerCount=F;function F(n){var e=this._events;if(e!==void 0){var t=e[n];if(typeof t=="function")return 1;if(t!==void 0)return t.length}return 0}u.prototype.eventNames=function(){return this._eventsCount>0?g(this._events):[]};function I(n,e){for(var t=new Array(e),r=0;r<e;++r)t[r]=n[r];return t}function G(n,e){for(;e+1<n.length;e++)n[e]=n[e+1];n.pop()}function U(n){for(var e=new Array(n.length),t=0;t<e.length;++t)e[t]=n[t].listener||n[t];return e}function K(n,e){return new Promise(function(t,r){function i(o){n.removeListener(e,a),r(o)}function a(){typeof n.removeListener=="function"&&n.removeListener("error",i),t([].slice.call(arguments))}j(n,e,a,{once:!0}),e!=="error"&&V(n,i,{once:!0})})}function V(n,e,t){typeof n.on=="function"&&j(n,"error",e,t)}function j(n,e,t,r){if(typeof n.on=="function")r.once?n.once(e,t):n.on(e,t);else if(typeof n.addEventListener=="function")n.addEventListener(e,function i(a){r.once&&n.removeEventListener(e,i),t(a)});else throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof n)}class k extends f.exports{}const p=class{constructor({name:e}){y(this,h,void 0);y(this,L,[]);y(this,b,{});y(this,v,void 0);N(this,v,new k),N(this,h,e)}get name(){return l(this,h)}addFromException(e){this.error(e.message+" captured in "+e.fileName+" at "+e.fileNumber+", stack: "+e.stack,[])}emergency(e,t){this.addRecord(this.getLevelFromName(s.EMERGENCY),e,t)}alert(e,t){this.addRecord(this.getLevelFromName(s.ALERT),e,t)}critical(e,t){this.addRecord(this.getLevelFromName(s.CRITICAL),e,t)}error(e,t){this.addRecord(this.getLevelFromName(s.ERROR),e,t)}warning(e,t){this.addRecord(this.getLevelFromName(s.WARN),e,t)}notice(e,t){this.addRecord(this.getLevelFromName(s.NOTICE),e,t)}info(e,t){this.addRecord(this.getLevelFromName(s.INFO),e,t)}debug(e,t){this.addRecord(this.getLevelFromName(s.DEBUG),e,t)}log(e,t,r){this.addRecord(e,t,r)}getRecords(){return l(this,L)}addRecord(e,t,r){try{if(!this.levelExists(e))throw new Error(`Level ${e} do not exist`);const i={message:t,context:r,level:e,levelName:p.LEVELS[e],channel:l(this,h),captureDateTime:new Date,addon:[],toString(){return`${this.captureDateTime.toISOString().slice(0,19).replace("T"," ")} | ${this.message}`}};return l(this,L).push(i),l(this,v).emit("add_record",i),!0}catch{return!1}}getLevelFromName(e){return p.LEVELS.indexOf(e)}levelExists(e){return typeof p.LEVELS[e]=="string"}levelNameExists(e){return p.LEVELS.indexOf(e)>0}registerListener(e,t){l(this,v).on(e,t)}addExtension(e,t){const r=new e(t);l(this,b)[r.name]=r;const i=r.register(this,t);Object.defineProperty(this,r.name,{value:i,writable:!1,enumerable:!1,configurable:!1})}};let w=p;return h=new WeakMap,L=new WeakMap,b=new WeakMap,v=new WeakMap,S(w,"LEVELS",[s.DEBUG,s.INFO,s.NOTICE,s.WARN,s.ERROR,s.CRITICAL,s.ALERT,s.EMERGENCY]),w});