UNPKG

m-web-logger

Version:

logger util for web browser

5 lines (4 loc) 6.42 kB
(function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(a=typeof globalThis<"u"?globalThis:a||self,d(a.MLogger={}))})(this,function(a){"use strict";a.LogLevel=void 0,function(t){t[t.all=0]="all",t[t.info=1]="info",t[t.warn=2]="warn",t[t.error=3]="error",t[t.slient=4]="slient"}(a.LogLevel||(a.LogLevel={})),a.QueryKey=void 0,function(t){t.level="log_level",t.filter="log_name"}(a.QueryKey||(a.QueryKey={}));function d(t,r,i,o,u,c){function v(_){if(_!==void 0&&typeof _!="function")throw new TypeError("Function expected");return _}for(var l=o.kind,e=l==="getter"?"get":l==="setter"?"set":"value",n=!r&&t?o.static?t:t.prototype:null,f=r||(n?Object.getOwnPropertyDescriptor(n,o.name):{}),s,w=!1,h=i.length-1;h>=0;h--){var p={};for(var g in o)p[g]=g==="access"?{}:o[g];for(var g in o.access)p.access[g]=o.access[g];p.addInitializer=function(_){if(w)throw new TypeError("Cannot add initializers after decoration has completed");c.push(v(_||null))};var m=(0,i[h])(l==="accessor"?{get:f.get,set:f.set}:f[e],p);if(l==="accessor"){if(m===void 0)continue;if(m===null||typeof m!="object")throw new TypeError("Object expected");(s=v(m.get))&&(f.get=s),(s=v(m.set))&&(f.set=s),(s=v(m.init))&&u.unshift(s)}else(s=v(m))&&(l==="field"?u.unshift(s):f[e]=s)}n&&Object.defineProperty(n,o.name,f),w=!0}function E(t,r,i){for(var o=arguments.length>2,u=0;u<r.length;u++)i=o?r[u].call(t,i):r[u].call(t);return o?i:void 0}function L(t,r,i){if(i||arguments.length===2)for(var o=0,u=r.length,c;o<u;o++)(c||!(o in r))&&(c||(c=Array.prototype.slice.call(r,0,o)),c[o]=r[o]);return t.concat(c||Array.prototype.slice.call(r))}typeof SuppressedError=="function"&&SuppressedError;var y=function(){function t(r,i,o){i===void 0&&(i=""),this.level=r,this.name=i,this.data=o,this.time=new Date}return Object.defineProperty(t.prototype,"logTime",{get:function(){return this.time},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"logLevel",{get:function(){return this.level},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"logName",{get:function(){return this.name},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"logData",{get:function(){return this.data},enumerable:!1,configurable:!0}),t}();function b(t){return function(r,i){function o(){for(var u=[],c=0;c<arguments.length;c++)u[c]=arguments[c];var v=!!O.filter,l=this.level<=t,e=O.filter.apply(O,L([this.config],u,!1));return l&&!v||l&&e?r==null?void 0:r.apply(this,u):null}return o}}function j(t){var r;return(r=new URLSearchParams(location.search).get(t))!==null&&r!==void 0?r:""}function S(){var t;return(t=a.LogLevel[j(a.QueryKey.level)])!==null&&t!==void 0?t:a.LogLevel.slient}function k(){return function(t){var r,i=j(a.QueryKey.filter);return((r=t.name)!==null&&r!==void 0?r:"").includes(i)}}function D(){return function(t){var r,i=(r={},r[a.LogLevel.slient]="",r[a.LogLevel.error]="ERROR",r[a.LogLevel.warn]="WARN",r[a.LogLevel.info]="INFO",r[a.LogLevel.all]="LOG",r),o=t.logTime.toLocaleString(),u=t.logLevel;return"[".concat(o,"] [").concat(i[u],"] ").concat(t.logName?"[".concat(t.logName,"]"):"")}}function P(){return function(t){return t.logData}}var O=function(){var t,r=[],i,o,u,c,v;return t=function(){function l(e,n){e===void 0&&(e={}),n===void 0&&(n={console,prepend:D(),formatData:P()}),this.config=(E(this,r),e),this.options=n,this.level=Object.values(a.LogLevel).includes(this.config.level)?this.config.level:S()}return Object.defineProperty(l,"filter",{get:function(){var e;return(e=this._filter)!==null&&e!==void 0?e:k()},set:function(e){this._filter=e},enumerable:!1,configurable:!0}),Object.defineProperty(l.prototype,"name",{get:function(){var e;return(e=this.config)===null||e===void 0?void 0:e.name},enumerable:!1,configurable:!0}),l.prototype.setLevel=function(e){this.level=e},l.prototype.setName=function(e){this.config.name=e},l.prototype.warn=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return this.print(new y(a.LogLevel.warn,this.name,e))},l.prototype.log=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return this.print(new y(a.LogLevel.all,this.name,e))},l.prototype.error=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return this.print(new y(a.LogLevel.error,this.name,e))},l.prototype.info=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return this.print(new y(a.LogLevel.info,this.name,e))},l.prototype.trace=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var f=this.createStack();return this.print(new y(a.LogLevel.all,this.name,[f].concat(e)))},l.prototype.createStack=function(){var e,n=((e=new Error().stack)!==null&&e!==void 0?e:"").replace(`Error `,""),f=n.split(` `);return f.splice(0,3),f.join(` `)},l.prototype.print=function(e){var n,f,s,w,h=e.logLevel,p=this.options.prepend(e),g=this.options.formatData(e);return h===a.LogLevel.warn?(n=this.options.console).warn.apply(n,L(["".concat(p," ")],g,!1)):h===a.LogLevel.error?(f=this.options.console).error.apply(f,L(["".concat(p," ")],g,!1)):h===a.LogLevel.info?(s=this.options.console).info.apply(s,L(["".concat(p," ")],g,!1)):(w=this.options.console).log.apply(w,L(["".concat(p," ")],g,!1))},l}(),function(){var l=typeof Symbol=="function"&&Symbol.metadata?Object.create(null):void 0;i=[b(a.LogLevel.warn)],o=[b(a.LogLevel.all)],u=[b(a.LogLevel.error)],c=[b(a.LogLevel.info)],v=[b(a.LogLevel.all)],d(t,null,i,{kind:"method",name:"warn",static:!1,private:!1,access:{has:function(e){return"warn"in e},get:function(e){return e.warn}},metadata:l},null,r),d(t,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:l},null,r),d(t,null,u,{kind:"method",name:"error",static:!1,private:!1,access:{has:function(e){return"error"in e},get:function(e){return e.error}},metadata:l},null,r),d(t,null,c,{kind:"method",name:"info",static:!1,private:!1,access:{has:function(e){return"info"in e},get:function(e){return e.info}},metadata:l},null,r),d(t,null,v,{kind:"method",name:"trace",static:!1,private:!1,access:{has:function(e){return"trace"in e},get:function(e){return e.trace}},metadata:l},null,r),l&&Object.defineProperty(t,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:l})}(),t}();a.LogEvent=y,a.Logger=O,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});