May 18, 2021 WeChat Mini Program Development Document
To help small program developers quickly troubleshoot bugs and locate problems, we have introduced real-time logging. S tarting with Base Library 2.7.1, developers can print logs through the interface provided, aggregate logs, and escalate them to the background of small programs in real time. Developers can go to the log query page from the small program management background "development-operations center-real-time log" to view the log information printed by the developer.
1, call the relevant interface. The interface to log is wx.getRealtimeLogManager, and in order to be compatible with older versions, it is recommended to encapsulate it with code such as encapsulating .js log file:
var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
module.exports = {
info() {
if (!log) return
log.info.apply(log, arguments)
},
warn() {
if (!log) return
log.warn.apply(log, arguments)
},
error() {
if (!log) return
log.error.apply(log, arguments)
},
setFilterMsg(msg) { // 从基础库2.7.3开始支持
if (!log || !log.setFilterMsg) return
if (typeof msg !== 'string') return
log.setFilterMsg(msg)
},
addFilterMsg(msg) { // 从基础库2.8.1开始支持
if (!log || !log.addFilterMsg) return
if (typeof msg !== 'string') return
log.addFilterMsg(msg)
}
}
2, in the specific location of the page to print logs:
var log = require('./log.js') // 引用上面的log.js文件
log.info('hello test hahaha') // 日志会和当前打开的页面关联,建议在页面的onHide、onShow等生命周期里面打
log.warn('warn')
log.error('error')
log.setFilterMsg('filterkeyword')
log.setFilterMsg('addfilterkeyword')
A complete example can be referred to in the code snippet: https://developers.weixin.qq.com/s/i42NbKmp76bJ
Log in to the small program management background, from the "development-gt; operations center-real-time log" to the log query page. Developers can query the specified user's log information by setting the time, microsuming/OpenID, page link, FilterMsg content (base library 2.7.3 and above support setFilter Msg).
Due to background resource constraints, the Live Log usage rules are as follows:
setFilterMsg('scene1')
and the log can be obtained by entering a scene1 query on the MP. F
or example, in the online process, a monitoring problem, according to TheFilter Msg filter the specific user logs in this scenario. F
ilterMsg only supports case letters.
If you need to add more than one keyword, we recommend using addFilter Msg instead of setFilter Msg.