求助官方 getRealtimeLogManager 5Kb长度的限制?
RealtimeLogManager.info()
日志内容,可以有任意多个。每次调用的参数的总大小不超过5Kb
现在看到超过5Kb就会显示 UserLog:fail Log Size xxxx Exceed.
我在客户端尝试截断,有时灵有时不灵,有些日志上看到了截断后的信息,有些依然是 UserLog:fail Log Size xxxx Exceed.;
1,请求官方在接口上自动截断,尽量显示日志信息;
2,望指教下 5Kb 应该怎么计算才能准确无误?
直接把 console.log 封装了 RealtimeLogManager
if (wx.getRealtimeLogManager) { const logger = wx.getRealtimeLogManager() let consoleLog=console.log function str2ab(str) { var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节 var bufView = new Uint16Array(buf); for ( var i = 0, strLen = str.length; i < strLen; i++) { bufView[i] = str.charCodeAt(i); } return buf; } function ab2str(buf) { return String.fromCharCode.apply( null , new Uint16Array(buf)); } function wlog(){ let argumentsX=[...arguments] return new Promise( function (success,fail) { let params= "" argumentsX.forEach((val,i)=>{ let str=JSON.stringify(val); if (!str) return ; params+=str }) let bs=str2ab(params.substr(0,1024*5)) logger.info(ab2str(bs.slice(0,1024*5)).toString()); success() }) } console.log= function () { consoleLog(...arguments) wlog(...arguments); } } |
4 回复
找了另一种截串,依然不是很灵光,时好时坏的,请大家指点
//app.js // import log from 'utils/realTimeLog' //ascii 返回长度 function ascLength(codeat){ let len=1 if (codeat>0x4000000){ len=6 } else if (codeat>0x200000){ len=5 } else if (codeat>65536){ len=4 } else if (codeat>2048){ len=3 } else { len=2 } return len } //简单计算字节长度 String.getBlength = function (str) { for ( var i = str.length, n = 0; i--; ) { n += ascLength(str.charCodeAt(i)); } return n; } //按指定字节截取字符串 String.cutByte = function (str,len,endstr){ var len = +len ,endstr = typeof (endstr) == 'undefined' ? "..." : endstr.toString(); function n2(a){ var n = a / 2 | 0; return (n > 0 ? n : 1)} //用于二分法查找 if (!(str+ "" ).length || !len || len<=0){ return "" ;} if ( this .getBlength(str) <= len){ return str;} //整个函数中最耗时的一个判断,欢迎优化 var lenS = len - this .getBlength(endstr) ,_lenS = 0 , _strl = 0 while (_strl <= lenS){ var _lenS1 = n2(lenS -_strl) _strl += this .getBlength(str.substr(_lenS,_lenS1)) _lenS += _lenS1 } return str.substr(0,_lenS-1) + endstr } if (wx.getRealtimeLogManager) { const logger = wx.getRealtimeLogManager() let consoleLog=console.log function wlog(){ let argumentsX=[...arguments] return new Promise( function (success,fail) { let params= "" argumentsX.forEach((val,i)=>{ let str=JSON.stringify(val); if (!str) return ; params+=str }) logger.info(String.cutByte(params,5000)); success() }) } console.log= function () { consoleLog(...arguments) wlog(...arguments); } } |