谢天谢地,小程序终于出这个功能了,小程序上线后,若是用户反馈问题真的很难排查问题,咱们可以收集的资料极其有限,小程序推出实时日志真的能够有效帮助定位问题。javascript
2.7.1
调用起来很是方便,获取小程序日志管理器 wx.getRealtimeLogManager
,而后经过 info
、warn
、error
等方法打印不一样类型的日志。java
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
if (!log) return;
log.info('hello, info');
log.warn('hello, warn');
log.error('hello, error');
复制代码
也能够经过设置过滤关键字来帮助日志筛选。小程序
log.setFilterMsg('filterkeyword');
log.addFilterMsg('addfilterkeyword');
复制代码
page
通用能力中每次都写一遍比较麻烦,还容易出错,因此能够进行一些封装。下面为 app.js
app
onLaunch() {
this.enhancePage();
},
// 加强Page能力,小程序不支持prototype的形式拓展能力
enhancePage() {
const oPage = Page;
Page = config => oPage(Object.assign(config, {
$logger: this.getLogger(),
}));
},
// 获取日志打印器
getLogger() {
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
return {
info: () => log && log.info.apply(log, arguments),
warn: () => log && log.warn.apply(log, arguments),
error: () => log && log.error.apply(log, arguments),
setFilterMsg: msg => log && log.setFilterMsg && log.setFilterMsg(msg),
addFilterMsg: msg => log && log.addFilterMsg && log.addFilterMsg(msg),
}
}
复制代码
在页面中进行使用运维
Page({
onLoad() {
this.$logger.info('just test');
}
})
复制代码