uni-log-sdk 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var K=Object.create;var y=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),X=(n,t)=>{for(var e in t)y(n,e,{get:t[e],enumerable:!0})},A=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of J(t))!F.call(n,i)&&i!==e&&y(n,i,{get:()=>t[i],enumerable:!(r=$(t,i))||r.enumerable});return n};var z=(n,t,e)=>(e=n!=null?K(Y(n)):{},A(t||!n||!n.__esModule?y(e,"default",{value:n,enumerable:!0}):e,n)),Q=n=>A(y({},"__esModule",{value:!0}),n);var T=V(c=>{"use strict";Object.defineProperty(c,"__esModule",{value:!0});c.PROTOCOL_LIMITS=c.INGEST_PATH=c.LOG_LEVELS=void 0;c.LOG_LEVELS=["debug","info","warn","error","crash"];c.INGEST_PATH="/ingest/v1/logs";c.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var j=V(p=>{"use strict";var Z=p&&p.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),ee=p&&p.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&Z(t,n,e)};Object.defineProperty(p,"__esModule",{value:!0});ee(T(),p)});var he={};X(he,{flush:()=>ue,getClient:()=>se,hookConsole:()=>q,init:()=>oe,report:()=>de,setConsoleReport:()=>ge,setDeviceName:()=>ce,ulog:()=>ae,ulogger:()=>W});module.exports=Q(he);var g=z(j());function u(){let n=globalThis.uni;return n!=null?n:null}function te(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function S(n){return typeof n=="string"&&n.length>0?n:void 0}function ne(n){let t=l=>{var h;try{return(h=l==null?void 0:l())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(S).find(Boolean),d=(...l)=>l.map(te).find(h=>h!==void 0),v=s(r.deviceId,e.deviceId,e.deviceUUID),m=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!v||!m?null:{deviceId:v,platform:m,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function B(){return{request(n){var e;let t=u();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=u())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=u())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return ne(u())}catch{return null}},getCurrentPage(){var n,t,e;try{let r=(t=(n=u())==null?void 0:n.getCurrentPages)==null?void 0:t.call(n),i=r==null?void 0:r[r.length-1];return(e=i==null?void 0:i.route)!=null?e:null}catch{return null}},getPlatformVersion(){let n=u(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=S(s.hostVersion))!=null?i:S(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=u())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=u())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var w="uni-log:pending",M=20,re="0.1.0";function f(n,t){return n.length>t?n.slice(0,t):n}function b(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function ie(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var P=class{constructor(t,e=B()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(ie(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,v,m,l,h,k,I,L,E,R,_,N,O,U,x,C,D;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(l=(m=this.deviceNameOverride)!=null?m:b((v=(d=this.options).getDeviceName)==null?void 0:v.call(d)))!=null?l:b(this.options.deviceName))!=null?h:b(e==null?void 0:e.deviceName),i={deviceId:(k=e==null?void 0:e.deviceId)!=null?k:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((R=e==null?void 0:e.net)!=null?R:(E=(L=this.options).getNet)!=null&&E.call(L))?{net:(O=e==null?void 0:e.net)!=null?O:(N=(_=this.options).getNet)==null?void 0:N.call(_)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(x=(U=this.options).getUserId)!=null&&x.call(U)?{userId:this.options.getUserId()}:{},...(D=(C=this.options).getNet)!=null&&D.call(C)?{net:this.options.getNet()}:{},sdkVer:re,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(w),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>M&&(r=r.slice(-M));try{this.adapter.setStorage(w,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(w);if(!t)return;this.adapter.setStorage(w,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function oe(n){return o==null||o.stop(),o=new P(n),o.start(),n.hookConsole!==!1&&q(),o}function se(){return o}function ae(n,t,e){o==null||o.log(n,t,e)}var W={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},H=new Set,G=!0;function ge(n){G=n}function q(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(H.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{G&&W[r](s.map(le).join(" ")),a(...s)},H.add(e)}}function le(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function de(n,t){o==null||o.report(n,t)}function ce(n){o==null||o.setDeviceName(n)}async function ue(){await(o==null?void 0:o.flush())}0&&(module.exports={flush,getClient,hookConsole,init,report,setConsoleReport,setDeviceName,ulog,ulogger});
|
|
1
|
+
"use strict";var K=Object.create;var m=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),X=(n,t)=>{for(var e in t)m(n,e,{get:t[e],enumerable:!0})},A=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of J(t))!F.call(n,i)&&i!==e&&m(n,i,{get:()=>t[i],enumerable:!(r=$(t,i))||r.enumerable});return n};var z=(n,t,e)=>(e=n!=null?K(Y(n)):{},A(t||!n||!n.__esModule?m(e,"default",{value:n,enumerable:!0}):e,n)),Q=n=>A(m({},"__esModule",{value:!0}),n);var T=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var j=V(h=>{"use strict";var Z=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),ee=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&Z(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});ee(T(),h)});var pe={};X(pe,{flush:()=>he,getClient:()=>ae,hookConsole:()=>q,init:()=>se,report:()=>ue,setConsoleReport:()=>le,setDeviceName:()=>ce,ulog:()=>ge,ulogger:()=>W});module.exports=Q(pe);var g=z(j());function p(){let n=globalThis.uni;return n!=null?n:null}function te(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function ne(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function S(n){return typeof n=="string"&&n.length>0?n:void 0}function re(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(S).find(Boolean),d=(...l)=>l.map(ne).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function B(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return re(p())}catch{return null}},getCurrentPage(){var n;try{let t=te(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=S(s.hostVersion))!=null?i:S(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var w="uni-log:pending",M=20,ie="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function b(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function oe(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var P=class{constructor(t,e=B()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(oe(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,k,I,L,E,R,_,N,O,U,x,C,D;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:b((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:b(this.options.deviceName))!=null?c:b(e==null?void 0:e.deviceName),i={deviceId:(k=e==null?void 0:e.deviceId)!=null?k:this.fallbackDeviceId(),platform:(I=e==null?void 0:e.platform)!=null?I:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((R=e==null?void 0:e.net)!=null?R:(E=(L=this.options).getNet)!=null&&E.call(L))?{net:(O=e==null?void 0:e.net)!=null?O:(N=(_=this.options).getNet)==null?void 0:N.call(_)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(x=(U=this.options).getUserId)!=null&&x.call(U)?{userId:this.options.getUserId()}:{},...(D=(C=this.options).getNet)!=null&&D.call(C)?{net:this.options.getNet()}:{},sdkVer:ie,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(w),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>M&&(r=r.slice(-M));try{this.adapter.setStorage(w,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(w);if(!t)return;this.adapter.setStorage(w,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function se(n){return o==null||o.stop(),o=new P(n),o.start(),n.hookConsole!==!1&&q(),o}function ae(){return o}function ge(n,t,e){o==null||o.log(n,t,e)}var W={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},H=new Set,G=!0;function le(n){G=n}function q(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(H.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{G&&W[r](s.map(de).join(" ")),a(...s)},H.add(e)}}function de(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ue(n,t){o==null||o.report(n,t)}function ce(n){o==null||o.setDeviceName(n)}async function he(){await(o==null?void 0:o.flush())}0&&(module.exports={flush,getClient,hookConsole,init,report,setConsoleReport,setDeviceName,ulog,ulogger});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(c=>{"use strict";Object.defineProperty(c,"__esModule",{value:!0});c.PROTOCOL_LIMITS=c.INGEST_PATH=c.LOG_LEVELS=void 0;c.LOG_LEVELS=["debug","info","warn","error","crash"];c.INGEST_PATH="/ingest/v1/logs";c.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(p=>{"use strict";var F=p&&p.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=p&&p.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(p,"__esModule",{value:!0});X(A(),p)});var g=Y(T());function u(){let n=globalThis.uni;return n!=null?n:null}function z(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Q(n){let t=l=>{var h;try{return(h=l==null?void 0:l())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(z).find(h=>h!==void 0),v=s(r.deviceId,e.deviceId,e.deviceUUID),m=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!v||!m?null:{deviceId:v,platform:m,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=u();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=u())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=u())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Q(u())}catch{return null}},getCurrentPage(){var n,t,e;try{let r=(t=(n=u())==null?void 0:n.getCurrentPages)==null?void 0:t.call(n),i=r==null?void 0:r[r.length-1];return(e=i==null?void 0:i.route)!=null?e:null}catch{return null}},getPlatformVersion(){let n=u(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=u())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=u())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var y="uni-log:pending",B=20,Z="0.1.0";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function ee(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(ee(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,v,m,l,h,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(l=(m=this.deviceNameOverride)!=null?m:S((v=(d=this.options).getDeviceName)==null?void 0:v.call(d)))!=null?l:S(this.options.deviceName))!=null?h:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:Z,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(y),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(y,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(y);if(!t)return;this.adapter.setStorage(y,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&ne(),o}function ue(){return o}function he(n,t,e){o==null||o.log(n,t,e)}var te={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function pe(n){H=n}function ne(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&te[r](s.map(re).join(" ")),a(...s)},M.add(e)}}function re(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function fe(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,ue as getClient,ne as hookConsole,ce as init,fe as report,pe as setConsoleReport,ve as setDeviceName,he as ulog,te as ulogger};
|
|
1
|
+
var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(h=>{"use strict";var F=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});X(A(),h)});var g=Y(T());function p(){let n=globalThis.uni;return n!=null?n:null}function z(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function Q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Z(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(Q).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Z(p())}catch{return null}},getCurrentPage(){var n;try{let t=z(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var m="uni-log:pending",B=20,ee="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function te(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(te(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:S((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:S(this.options.deviceName))!=null?c:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:ee,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(m),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(m,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(m);if(!t)return;this.adapter.setStorage(m,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&re(),o}function he(){return o}function pe(n,t,e){o==null||o.log(n,t,e)}var ne={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function fe(n){H=n}function re(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&ne[r](s.map(ie).join(" ")),a(...s)},M.add(e)}}function ie(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ye(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,he as getClient,re as hookConsole,ce as init,ye as report,fe as setConsoleReport,ve as setDeviceName,pe as ulog,ne as ulogger};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(c=>{"use strict";Object.defineProperty(c,"__esModule",{value:!0});c.PROTOCOL_LIMITS=c.INGEST_PATH=c.LOG_LEVELS=void 0;c.LOG_LEVELS=["debug","info","warn","error","crash"];c.INGEST_PATH="/ingest/v1/logs";c.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(p=>{"use strict";var F=p&&p.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=p&&p.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(p,"__esModule",{value:!0});X(A(),p)});var g=Y(T());function u(){let n=globalThis.uni;return n!=null?n:null}function z(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Q(n){let t=l=>{var h;try{return(h=l==null?void 0:l())!=null?h:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(z).find(h=>h!==void 0),v=s(r.deviceId,e.deviceId,e.deviceUUID),m=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!v||!m?null:{deviceId:v,platform:m,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=u();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=u())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=u())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Q(u())}catch{return null}},getCurrentPage(){var n,t,e;try{let r=(t=(n=u())==null?void 0:n.getCurrentPages)==null?void 0:t.call(n),i=r==null?void 0:r[r.length-1];return(e=i==null?void 0:i.route)!=null?e:null}catch{return null}},getPlatformVersion(){let n=u(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=u())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=u())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var y="uni-log:pending",B=20,Z="0.1.0";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function ee(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(ee(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,v,m,l,h,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(h=(l=(m=this.deviceNameOverride)!=null?m:S((v=(d=this.options).getDeviceName)==null?void 0:v.call(d)))!=null?l:S(this.options.deviceName))!=null?h:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:Z,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(y),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(y,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(y);if(!t)return;this.adapter.setStorage(y,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&ne(),o}function ue(){return o}function he(n,t,e){o==null||o.log(n,t,e)}var te={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function pe(n){H=n}function ne(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&te[r](s.map(re).join(" ")),a(...s)},M.add(e)}}function re(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function fe(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,ue as getClient,ne as hookConsole,ce as init,fe as report,pe as setConsoleReport,ve as setDeviceName,he as ulog,te as ulogger};
|
|
1
|
+
var W=Object.create;var D=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var V=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports);var J=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of q(t))!$.call(n,i)&&i!==e&&D(n,i,{get:()=>t[i],enumerable:!(r=G(t,i))||r.enumerable});return n};var Y=(n,t,e)=>(e=n!=null?W(K(n)):{},J(t||!n||!n.__esModule?D(e,"default",{value:n,enumerable:!0}):e,n));var A=V(u=>{"use strict";Object.defineProperty(u,"__esModule",{value:!0});u.PROTOCOL_LIMITS=u.INGEST_PATH=u.LOG_LEVELS=void 0;u.LOG_LEVELS=["debug","info","warn","error","crash"];u.INGEST_PATH="/ingest/v1/logs";u.PROTOCOL_LIMITS={maxBatch:50,maxMsgLength:4e3,maxStackLength:8e3,maxEventsPerBatch:50,maxEventKeyLength:64,maxEventValLength:255,maxBodyBytes:512*1024}});var T=V(h=>{"use strict";var F=h&&h.__createBinding||(Object.create?(function(n,t,e,r){r===void 0&&(r=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(n,r,i)}):(function(n,t,e,r){r===void 0&&(r=e),n[r]=t[e]})),X=h&&h.__exportStar||function(n,t){for(var e in n)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&F(t,n,e)};Object.defineProperty(h,"__esModule",{value:!0});X(A(),h)});var g=Y(T());function p(){let n=globalThis.uni;return n!=null?n:null}function z(){var n,t,e;try{let r=globalThis;return typeof r.getCurrentPages=="function"?r.getCurrentPages():typeof getCurrentPages=="function"?getCurrentPages():(e=(t=(n=r.uni)==null?void 0:n.getCurrentPages)==null?void 0:t.call(n))!=null?e:null}catch{return null}}function Q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function P(n){return typeof n=="string"&&n.length>0?n:void 0}function Z(n){let t=l=>{var c;try{return(c=l==null?void 0:l())!=null?c:{}}catch{return{}}},e=t(n==null?void 0:n.getSystemInfoSync),r=t(n==null?void 0:n.getDeviceInfo),i=t(n==null?void 0:n.getAppBaseInfo),a=t(n==null?void 0:n.getWindowInfo);if(Object.keys(e).length===0&&Object.keys(r).length===0)return null;let s=(...l)=>l.map(P).find(Boolean),d=(...l)=>l.map(Q).find(c=>c!==void 0),y=s(r.deviceId,e.deviceId,e.deviceUUID),v=s(e.uniPlatform,e.platform,r.platform,r.uniPlatform);return!y||!v?null:{deviceId:y,platform:v,deviceName:s(r.deviceName,e.deviceName),model:s(r.deviceModel,e.deviceModel,e.model),vendor:s(r.deviceBrand,e.deviceBrand,e.brand),os:s(e.osVersion,e.system,r.osVersion,r.system),osVersion:s(e.osVersion,r.osVersion),osName:s(e.osName,r.osName,i.osName),net:s(e.networkType,e.netType),language:s(i.language,e.language),pixelRatio:d(a.pixelRatio,e.pixelRatio),windowWidth:d(a.windowWidth,e.windowWidth),windowHeight:d(a.windowHeight,e.windowHeight),screenWidth:d(a.screenWidth,e.screenWidth),screenHeight:d(a.screenHeight,e.screenHeight)}}function j(){return{request(n){var e;let t=p();if(!(t!=null&&t.request)){(e=n.fail)==null||e.call(n,{errMsg:"uni-log: uni.request \u4E0D\u53EF\u7528"});return}t.request({...n,method:"POST"})},getStorage(n){var t,e;try{let r=(e=(t=p())==null?void 0:t.getStorageSync)==null?void 0:e.call(t,n);return typeof r=="string"&&r.length>0?r:null}catch{return null}},setStorage(n,t){var e,r;try{(r=(e=p())==null?void 0:e.setStorageSync)==null||r.call(e,n,t)}catch{}},getDeviceInfo(){try{return Z(p())}catch{return null}},getCurrentPage(){var n;try{let t=z(),e=t==null?void 0:t[t.length-1];return(n=e==null?void 0:e.route)!=null?n:null}catch{return null}},getPlatformVersion(){let n=p(),t=r=>{var i,a;try{let s=r();return s&&(a=(i=P(s.hostVersion))!=null?i:P(s.version))!=null?a:null}catch{return null}},e=n!=null&&n.getAppBaseInfo?t(()=>n.getAppBaseInfo()):null;return e||(n!=null&&n.getSystemInfoSync?t(()=>n.getSystemInfoSync()):null)},onError(n){var t,e;try{(e=(t=p())==null?void 0:t.onError)==null||e.call(t,n)}catch{}},onUnhandledRejection(n){var t,e;try{(e=(t=p())==null?void 0:t.onUnhandledRejection)==null||e.call(t,n)}catch{}}}}var m="uni-log:pending",B=20,ee="0.1.1";function f(n,t){return n.length>t?n.slice(0,t):n}function S(n){let t=n==null?void 0:n.trim();return t?f(t,128):void 0}function te(n,t,e){var i;let r={t:Date.now(),lv:n,msg:f(t||"(empty)",g.PROTOCOL_LIMITS.maxMsgLength)};return e!=null&&e.stack&&(r.stack=f(e.stack,g.PROTOCOL_LIMITS.maxStackLength)),e!=null&&e.page&&(r.page=f(e.page,255)),e!=null&&e.extra&&(r.extra=e.extra),(i=e==null?void 0:e.tags)!=null&&i.length&&(r.tags=e.tags),r}var w=class{constructor(t,e=j()){this.ready=!1;this.logs=[];this.events=[];this.timer=null;this.flushingPromise=null;this.launchTime=Date.now();this.pageCount=0;this.lastPage=null;this.deviceInfo=null;this.deviceNameOverride=null;this.options=t,this.adapter=e,this.sessionId=`sess-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}start(){var e;if(this.ready)return;this.ready=!0;let t=Math.max(1e3,(e=this.options.flushInterval)!=null?e:1e4);this.timer=setInterval(()=>{this.flush().catch(()=>{})},t),this.retryPending().catch(()=>{}),this.options.collectErrors!==!1&&this.installErrorHooks(),this.samplePage()}stop(){this.ready=!1,this.timer&&(clearInterval(this.timer),this.timer=null)}log(t,e,r){if(!this.ready)return;let i=g.LOG_LEVELS.includes(t)?t:"info";this.samplePage();let a=this.adapter.getCurrentPage();this.logs.push(te(i,e,{...r,...a?{page:a}:{}})),this.logs.length>=g.PROTOCOL_LIMITS.maxBatch&&this.flush()}report(t,e){if(!this.ready||!t||t.length>g.PROTOCOL_LIMITS.maxEventKeyLength)return;this.samplePage();let r={t:Date.now(),ev:t};e&&(r.val=f(e,g.PROTOCOL_LIMITS.maxEventValLength));let i=this.adapter.getCurrentPage();i&&(r.page=i),this.events.push(r),this.events.length>=g.PROTOCOL_LIMITS.maxEventsPerBatch&&this.flush()}pageView(t){if(!this.ready)return;let e=t!=null?t:this.adapter.getCurrentPage();this.pageCount+=1,e&&(this.lastPage=e)}setDeviceName(t){let e=t.trim();this.deviceNameOverride=e?f(e,128):null}samplePage(){let t=this.adapter.getCurrentPage();t&&t!==this.lastPage&&(this.lastPage=t,this.pageCount+=1)}async flush(){if(!this.ready)return;if(this.flushingPromise)return this.flushingPromise;let t=async()=>{var r;let e;try{for(this.samplePage(),e=this.buildPayload(!0);;){if(!await this.send(e)){(e.logs.length||(r=e.events)!=null&&r.length)&&(this.persistPending([e]),this.options.debug&&console.warn("[uni-log] \u4E0A\u62A5\u5931\u8D25\uFF0C\u5DF2\u6682\u5B58\u5F85\u7EED\u4F20"));break}if(this.logs.length===0&&this.events.length===0)break;e=this.buildPayload(!1)}}catch(i){if(e)try{this.persistPending([e])}catch{}this.options.debug&&console.warn("[uni-log] flush \u5F02\u5E38",i)}finally{this.flushingPromise=null}};return this.flushingPromise=t(),this.flushingPromise}buildPayload(t){var s,d,y,v,l,c,b,k,I,L,E,R,_,N,O,U,x,C;let e=(s=this.deviceInfo)!=null?s:this.deviceInfo=this.adapter.getDeviceInfo(),r=(c=(l=(v=this.deviceNameOverride)!=null?v:S((y=(d=this.options).getDeviceName)==null?void 0:y.call(d)))!=null?l:S(this.options.deviceName))!=null?c:S(e==null?void 0:e.deviceName),i={deviceId:(b=e==null?void 0:e.deviceId)!=null?b:this.fallbackDeviceId(),platform:(k=e==null?void 0:e.platform)!=null?k:"unknown",...r?{deviceName:r}:{},...e!=null&&e.model?{model:e.model}:{},...e!=null&&e.vendor?{vendor:e.vendor}:{},...e!=null&&e.os?{os:e.os}:{},...e!=null&&e.osVersion?{osVersion:e.osVersion}:{},...e!=null&&e.osName?{osName:e.osName}:{},...((E=e==null?void 0:e.net)!=null?E:(L=(I=this.options).getNet)!=null&&L.call(I))?{net:(N=e==null?void 0:e.net)!=null?N:(_=(R=this.options).getNet)==null?void 0:_.call(R)}:{},...e!=null&&e.language?{language:e.language}:{},...e!=null&&e.pixelRatio?{pixelRatio:e.pixelRatio}:{},...e!=null&&e.windowWidth?{windowWidth:e.windowWidth}:{},...e!=null&&e.windowHeight?{windowHeight:e.windowHeight}:{},...e!=null&&e.screenWidth?{screenWidth:e.screenWidth}:{},...e!=null&&e.screenHeight?{screenHeight:e.screenHeight}:{}};return{header:{appKey:this.options.appKey,...this.options.appVer?{appVer:this.options.appVer}:{},...this.options.release?{release:this.options.release}:{},...this.options.channel?{channel:this.options.channel}:{},sessionId:this.sessionId,...(U=(O=this.options).getUserId)!=null&&U.call(O)?{userId:this.options.getUserId()}:{},...(C=(x=this.options).getNet)!=null&&C.call(x)?{net:this.options.getNet()}:{},sdkVer:ee,...this.adapter.getPlatformVersion()?{platformVersion:this.adapter.getPlatformVersion()}:{}},device:i,...t?{session:{launchTime:this.launchTime,duration:Math.max(0,Math.round((Date.now()-this.launchTime)/1e3)),pageCount:this.pageCount}}:{},logs:this.logs.splice(0,g.PROTOCOL_LIMITS.maxBatch),...this.events.length?{events:this.events.splice(0,g.PROTOCOL_LIMITS.maxEventsPerBatch)}:{}}}fallbackDeviceId(){let t=this.adapter.getStorage("uni-log:did");return t||(t=`did-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,10)}`,this.adapter.setStorage("uni-log:did",t)),t}send(t){return new Promise(e=>{let r=!1,i=s=>{r||(r=!0,clearTimeout(a),e(s))},a=setTimeout(()=>i(!1),8e3);try{this.adapter.request({url:`${this.options.serverUrl}${g.INGEST_PATH}`,data:t,header:{"Content-Type":"application/json"},success:s=>i(!!s&&s.statusCode>=200&&s.statusCode<300),fail:()=>i(!1)})}catch{i(!1)}})}safeStringify(t){let e=new WeakSet;return JSON.stringify(t,(r,i)=>{if(typeof i=="bigint")return i.toString();if(typeof i=="object"&&i!==null){if(e.has(i))return;e.add(i)}return i})}persistPending(t){let e=this.adapter.getStorage(m),r=[];try{r=e?JSON.parse(e):[],Array.isArray(r)||(r=[])}catch{r=[]}r.push(...t),r.length>B&&(r=r.slice(-B));try{this.adapter.setStorage(m,this.safeStringify(r))}catch(i){this.options.debug&&console.warn("[uni-log] \u6682\u5B58\u5931\u8D25",i)}}async retryPending(){let t=this.adapter.getStorage(m);if(!t)return;this.adapter.setStorage(m,"");let e;try{e=JSON.parse(t)}catch{return}if(!(!Array.isArray(e)||e.length===0)){for(let r=0;r<e.length;r++)if(!await this.send(e[r])){this.persistPending(e.slice(r)),this.options.debug&&console.warn("[uni-log] \u7EED\u4F20\u5931\u8D25\uFF0C\u6682\u5B58\u5DF2\u8FD8\u539F");return}}}installErrorHooks(){this.adapter.onError(t=>{this.log("error",String(t),{stack:String(t)})}),this.adapter.onUnhandledRejection(t=>{var i;let e=t,r=(i=e==null?void 0:e.reason)!=null?i:t;this.log("error",String(r),{stack:r==null?void 0:r.stack})})}};var o=null;function ce(n){return o==null||o.stop(),o=new w(n),o.start(),n.hookConsole!==!1&&re(),o}function he(){return o}function pe(n,t,e){o==null||o.log(n,t,e)}var ne={debug:(n,t)=>o==null?void 0:o.log("debug",n,t),info:(n,t)=>o==null?void 0:o.log("info",n,t),warn:(n,t)=>o==null?void 0:o.log("warn",n,t),error:(n,t)=>o==null?void 0:o.log("error",n,t)},M=new Set,H=!0;function fe(n){H=n}function re(){let n={debug:"debug",log:"info",info:"info",warn:"warn",error:"error"},t=console;for(let[e,r]of Object.entries(n)){if(M.has(e))continue;let i=t[e];if(typeof i!="function")continue;let a=i.bind(console);t[e]=(...s)=>{H&&ne[r](s.map(ie).join(" ")),a(...s)},M.add(e)}}function ie(n){if(typeof n=="string")return n;if(n instanceof Error)return n.stack||n.message;try{let t=JSON.stringify(n);return t===void 0?String(n):t}catch{return String(n)}}function ye(n,t){o==null||o.report(n,t)}function ve(n){o==null||o.setDeviceName(n)}async function me(){await(o==null?void 0:o.flush())}export{me as flush,he as getClient,re as hookConsole,ce as init,ye as report,fe as setConsoleReport,ve as setDeviceName,pe as ulog,ne as ulogger};
|