An overview of the
Thankfully, miniprogram finally has this function. After the launch of the miniprogram, if the user feedback is really difficult to troubleshoot the problem, we can collect very limited data, and the real-time log of the miniprogram can effectively help locate the problem.
The environment that
- Basic library:
2.7.1
- Log query address: Development > O&M Center > real-time logs. The experience version can also be uploaded, but it must be on the real machine
How can
Get the small program log manager wx.getRealtimeLogManager, and print different types of logs through the info, WARN, error, and other methods.
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
if(! log)return;
log.info('hello, info');
log.warn('hello, warn');
log.error('hello, error');
Copy the code
You can also set filter keywords to help log filtering.
log.setFilterMsg('filterkeyword');
log.addFilterMsg('addfilterkeyword');
Copy the code
Wrapped inpage
In general capability
Writing it every time is cumbersome and error-prone, so you can do some encapsulation. Below for app. Js
onLaunch() {
this.enhancePage();
},
// Enhance Page capabilities, small programs do not support prototype form extension capabilities
enhancePage() {
const oPage = Page;
Page = config= > oPage(Object.assign(config, {
$logger: this.getLogger(),
}));
},
// Get the log printer
getLogger() {
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
return {
info: (a)= > log && log.info.apply(log, arguments),
warn: (a)= > log && log.warn.apply(log, arguments),
error: (a)= > log && log.error.apply(log, arguments),
setFilterMsg: msg= > log && log.setFilterMsg && log.setFilterMsg(msg),
addFilterMsg: msg= > log && log.addFilterMsg && log.addFilterMsg(msg),
}
}
Copy the code
Use it on the page
Page({
onLoad() {
this.$logger.info('just test'); }})Copy the code
Matters needing attention
- To facilitate fault locating, logs are divided by page. Logs generated between onShow and onHide (switching to other pages and retreating to the background with the dot in the upper right corner) on a page are aggregated into a log and reported. You can search for this log on the mini program management console based on the page path.
- Each mini program account is limited to 1 million logs per day. Logs are retained for three days. You are advised to locate problems in time.
- The maximum size of a log is 5KB and contains a maximum of 200 log function calls (including info, warn, and error calls). Therefore, be cautious about logging. Avoid calling the log interface in the loop and overwriting console.log directly.
- You can search for logs based on OpenID.
- SetFilterMsg Specifies the Msg that can be filtered. This interface provides filtering capability for a certain scenario. For example, setFilterMsg(‘scene1’). You can enter scene1 on MP to query the log. For example, if a monitoring problem occurs during the online process, you can filter specific user logs in this scenario based on FilterMsg. FilterMsg supports only upper and lower case letters. If you need to add multiple keywords, you are advised to use addFilterMsg instead of setFilterMsg.