Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program real-time log


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Real-time logs

Background

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.

How to use it

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

How to view the logs

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).

WeChat program real-time log

Precautions

Due to background resource constraints, the Live Log usage rules are as follows:

  1. In order to locate the problem conveniently, the log is divided by page, a page, between the onShow and onHide (switch to other pages, the upper right corner dot back to the background) between the log, will be aggregated into a log escalation, and in the small program management back desk can be based on the page path search out the log.
  2. Each small program account is limited to 5 million logs per day, logs will be retained for 7 days, it is recommended to encounter problems in a timely manner.
  3. The upper limit of a log is 5KB and contains up to 200 call to the print log function (info, warn, error calls are counted), so be careful to log, avoid calling the log interface in the loop, and avoid logging directly by rewriting the console.log way.
  4. Comment feedback inside the log, according to OpenID search log.
  5. SetFilter Msg can set the filtered Msg. T he purpose of this interface is to provide 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.

WeChat program real-time log