UNPKG

3.98 kBJavaScriptView Raw
1/*!
2 * @mp-logger/logger v0.0.1 (https://github.com/Barrior/mp-logger)
3 * Released under the MIT License.
4 */
5
6!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).mpLogger=e()}(this,(function(){"use strict";var t=function(){return(t=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function e(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var o=Array(t),r=0;for(e=0;e<n;e++)for(var i=arguments[e],a=0,s=i.length;a<s;a++,r++)o[r]=i[a];return o}var n="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},o="object"==typeof n&&n&&n.Object===Object&&n,r="object"==typeof self&&self&&self.Object===Object&&self,i=(o||r||Function("return this")()).Symbol,a=Object.prototype,s=a.hasOwnProperty,l=a.toString,u=i?i.toStringTag:void 0;var f=function(t){var e=s.call(t,u),n=t[u];try{t[u]=void 0;var o=!0}catch(t){}var r=l.call(t);return o&&(e?t[u]=n:delete t[u]),r},c=Object.prototype.toString;var g=function(t){return c.call(t)},p=i?i.toStringTag:void 0;var d=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":p&&p in Object(t)?f(t):g(t)};var h=function(t){return null!=t&&"object"==typeof t};var y=function(t,e){return function(n){return t(e(n))}}(Object.getPrototypeOf,Object),b=Function.prototype,v=Object.prototype,j=b.toString,O=v.hasOwnProperty,w=j.call(Object);var m=function(t){if(!h(t)||"[object Object]"!=d(t))return!1;var e=y(t);if(null===e)return!0;var n=O.call(e,"constructor")&&e.constructor;return"function"==typeof n&&n instanceof n&&j.call(n)==w};var T,S=function(t){if(!h(t))return!1;var e=d(t);return"[object Error]"==e||"[object DOMException]"==e||"string"==typeof t.message&&"string"==typeof t.name&&!m(t)};return function(t){t[t.debug=0]="debug",t[t.log=1]="log",t[t.info=2]="info",t[t.warn=3]="warn",t[t.error=4]="error"}(T||(T={})),new(function(){function n(){this.setting=n.defaultSetting,this.logs=[],this.apiAddress="/cgi/logs"}return n.prototype.initSetting=function(e){if(this.setting=t(t({},n.defaultSetting),e),!this.setting.requestClient)throw new Error("缺少 HTTP 客户端,请添加 request 参数");var o,r,i;this.sendDataDebounced=(o=this.sendData,r=this.setting.debounceTime,function(){for(var t=this,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var a=function(){return o.apply(t,e)};clearTimeout(i),i=setTimeout(a,r)})},n.prototype.sendData=function(){var n,o=t(t({timeout:1e4},this.setting.requestOptions),{url:this.setting.origin+this.apiAddress,method:"POST",data:{d:e(this.logs)}});(null!==(n=o.header)&&void 0!==n?n:o.header={})["content-type"]="application/json",this.setting.requestClient(o),this.logs.length=0},n.prototype.handleLog=function(t,e){var n;if(this.setting.sendData){var o=[];e.forEach((function(t){S(t)?o.push({$$e:1,msg:t.message,stack:t.stack}):o.push(t)})),this.logs.push({t:Date.now(),l:t,m:o}),null===(n=this.sendDataDebounced)||void 0===n||n.call(this)}else console[T[t]].apply(console,e)},n.prototype.debug=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handleLog(T.debug,t)},n.prototype.log=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handleLog(T.log,t)},n.prototype.info=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handleLog(T.info,t)},n.prototype.warn=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handleLog(T.warn,t)},n.prototype.error=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handleLog(T.error,t)},n.defaultSetting={origin:"http://localhost",sendData:!1,debounceTime:100,requestClient:"undefined"!=typeof wx&&wx.request,requestOptions:{}},n}())}));