UNPKG

m-web-logger

Version:

logger util for web browser

2 lines (1 loc) 5.6 kB
(function(n,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(n=typeof globalThis<"u"?globalThis:n||self,g(n.MLogger={}))})(this,function(n){"use strict";function g(r,t,c,o,l,u){function i(L){if(L!==void 0&&typeof L!="function")throw new TypeError("Function expected");return L}for(var e=o.kind,a=e==="getter"?"get":e==="setter"?"set":"value",v=!t&&r?o.static?r:r.prototype:null,s=t||(v?Object.getOwnPropertyDescriptor(v,o.name):{}),f,E=!1,O=c.length-1;O>=0;O--){var _={};for(var y in o)_[y]=y==="access"?{}:o[y];for(var y in o.access)_.access[y]=o.access[y];_.addInitializer=function(L){if(E)throw new TypeError("Cannot add initializers after decoration has completed");u.push(i(L||null))};var h=(0,c[O])(e==="accessor"?{get:s.get,set:s.set}:s[a],_);if(e==="accessor"){if(h===void 0)continue;if(h===null||typeof h!="object")throw new TypeError("Object expected");(f=i(h.get))&&(s.get=f),(f=i(h.set))&&(s.set=f),(f=i(h.init))&&l.unshift(f)}else(f=i(h))&&(e==="field"?l.unshift(f):s[a]=f)}v&&Object.defineProperty(v,o.name,s),E=!0}function P(r,t,c){for(var o=arguments.length>2,l=0;l<t.length;l++)c=o?t[l].call(r,c):t[l].call(r);return o?c:void 0}function S(r,t,c){if(c||arguments.length===2)for(var o=0,l=t.length,u;o<l;o++)(u||!(o in t))&&(u||(u=Array.prototype.slice.call(t,0,o)),u[o]=t[o]);return r.concat(u||Array.prototype.slice.call(t))}typeof SuppressedError=="function"&&SuppressedError;function j(r){var t;return(t=new URLSearchParams(location.search).get(r))!==null&&t!==void 0?t:""}function Q(r){switch(typeof r){case"number":return Number.isInteger(r)?"%i":"%f";case"function":return"%O";case"object":return"%o";default:return"%s"}}var d,p;n.LogLevel=void 0,function(r){r[r.all=0]="all",r[r.info=1]="info",r[r.warn=2]="warn",r[r.error=3]="error",r[r.slient=4]="slient"}(n.LogLevel||(n.LogLevel={})),n.QueryKey=void 0,function(r){r.level="log_level",r.filter="label_filter"}(n.QueryKey||(n.QueryKey={}));var D=(d={},d[n.LogLevel.slient]="",d[n.LogLevel.error]="red",d[n.LogLevel.warn]="orange",d[n.LogLevel.info]="skyblue",d[n.LogLevel.all]="gray",d),T=(p={},p[n.LogLevel.slient]="",p[n.LogLevel.error]="[error]",p[n.LogLevel.warn]="[warn]",p[n.LogLevel.info]="[info]",p[n.LogLevel.all]="[log]",p),k=function(){var r;return(r=n.LogLevel[j(n.QueryKey.level)])!==null&&r!==void 0?r:n.LogLevel.slient},F=function(){return function(r){var t,c=j(n.QueryKey.filter);return((t=r.label)!==null&&t!==void 0?t:"").includes(c)}};function m(r,t){var c=t.name;function o(){for(var l=[],u=0;u<arguments.length;u++)l[u]=arguments[u];return l.length||console.error("Missing required argument in ".concat(String(c),".")),r==null?void 0:r.apply(this,l)}return o}function b(r){return function(t,c){function o(){for(var l,u=[],i=0;i<arguments.length;i++)u[i]=arguments[i];var e=!!w.filter,a=this.level<=r,v=w.filter.apply(w,S([(l=this.config)!==null&&l!==void 0?l:{}],u,!1));return a&&!e||a&&v?(this.triggerInterceptors(r,u),t==null?void 0:t.apply(this,u)):null}return o}}var w=function(){var r,t=[],c,o,l,u;return r=function(){function i(e){e===void 0&&(e={}),this.config=(P(this,t),e),this.level=k(),this.level=Object.values(n.LogLevel).includes(this.config.level)?this.config.level:k()}return Object.defineProperty(i.prototype,"label",{get:function(){var e;return(e=this.config)===null||e===void 0?void 0:e.label},enumerable:!1,configurable:!0}),i.useInterceptor=function(e){r.interceptors.push(e)},i.removeInterceptor=function(e){var a=r.interceptors.findIndex(function(v){return v===e});a>=0&&r.interceptors.splice(a,1)},i.prototype.setLevel=function(e){this.level=e},i.prototype.setLabel=function(e){this.config.label=e},i.prototype.warn=function(){for(var e=[],a=0;a<arguments.length;a++)e[a]=arguments[a];return this.print(n.LogLevel.warn,e)},i.prototype.log=function(){for(var e=[],a=0;a<arguments.length;a++)e[a]=arguments[a];return this.print(n.LogLevel.all,e)},i.prototype.error=function(){for(var e=[],a=0;a<arguments.length;a++)e[a]=arguments[a];return this.print(n.LogLevel.error,e)},i.prototype.info=function(){for(var e=[],a=0;a<arguments.length;a++)e[a]=arguments[a];return this.print(n.LogLevel.info,e)},i.prototype.triggerInterceptors=function(e,a){var v=this,s;(s=r.interceptors)===null||s===void 0||s.forEach(function(f){f({callLevel:e,config:v.config},a)})},i.prototype.print=function(e,a){var v=this.label?"[".concat(this.label,"]-").concat(T[e]):T[e],s=D[e],f=[];return a.forEach(function(E){f.push(Q(E))}),console.log.apply(console,S(["%c".concat(v,": ").concat(f.join(" ")),"color: ".concat(s)],a,!1))},i}(),function(){var i=typeof Symbol=="function"&&Symbol.metadata?Object.create(null):void 0;c=[m,b(n.LogLevel.warn)],o=[m,b(n.LogLevel.all)],l=[m,b(n.LogLevel.error)],u=[m,b(n.LogLevel.info)],g(r,null,c,{kind:"method",name:"warn",static:!1,private:!1,access:{has:function(e){return"warn"in e},get:function(e){return e.warn}},metadata:i},null,t),g(r,null,o,{kind:"method",name:"log",static:!1,private:!1,access:{has:function(e){return"log"in e},get:function(e){return e.log}},metadata:i},null,t),g(r,null,l,{kind:"method",name:"error",static:!1,private:!1,access:{has:function(e){return"error"in e},get:function(e){return e.error}},metadata:i},null,t),g(r,null,u,{kind:"method",name:"info",static:!1,private:!1,access:{has:function(e){return"info"in e},get:function(e){return e.info}},metadata:i},null,t),i&&Object.defineProperty(r,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:i})}(),r.interceptors=[],r.filter=F(),r}();n.Logger=w,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});