# Live Logs

# background

To help Weixin Mini Program developers quickly troubleshoot Mini Programs vulnerabilities and locate problems, we introduced real-time logging. Developers can print logs through the interface provided, and the logs are aggregated and reported in real time to the Mini Program's background.Developers can go to the Mini Program-side log query page from We Analysing "Performance Quality - > Live Logs - > App Logs" or to the plug-in log query pages from "Performance Quality -- > Live Log - > Plugin Logs" to see log information printed by the developer.

# How to use it

# Weixin Mini Program / MiniGame end

Starting with the base library2.7.1, the Weixin Mini Program end can use real-time logs, and the MiniGame end is supported from the base library2.1.4.4.

  1. Call the relevant interface.The interface for logging iswx.getRealtimeLogManager,For compatibility with older versions, it is recommended to wrap it in thelog.jsfile:
var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null

module.exports = {
  debug() {
    if (!log) return
    log.debug.apply(log, arguments)
  },
  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)
  }
}

Print the log at the specific location of the page:

var log = require('./log.js') // 引用上面的log.js文件
log.info('hello test hahaha') // 日志会和当前打开的页面关联,建议在页面的onHide、onShow等生命周期里面打
log.warn('warn')
log.error('error')
log.setFilterMsg('filterkeyword')
log.addFilterMsg('addfilterkeyword')

The full example can be found in the code snippet: https://developers.weixin.qq.com/s/aFYw1BmC7eak

# The plug-in end

Starting with the base library2.16.0, the plug-in side also supports real-time logging.In order to make the logs more structured so that more complex analysis can be performed subsequently, the plug-in side uses a newly designed format.

  1. Call the relevant interfacewx.getRealtimeLogManagerto get the real-time log manager instance:
const logManager = wx.getRealtimeLogManager()
  1. In the logic that needs to log, obtain a log instance:
// Tag names can be any character string, one tag name corresponds to a set of logs; The same label name is allowed to be reused, and logs with the same label name are summarized under a single label in the background
// Tags can be classified as logs, so developers are advised to logically categorize tags
const logger = logManager.tag('plugin-onUserTapSth')
  1. Print the log in the appropriate place:
logger.info('key1', 'value1') // 每条日志为一个 key-value 对,key 必须是字符串,value 可以是字符串/数值/对象/数组等可序列化类型
logger.error('key2', {str: 'value2'})
logger.warn('key3', 'value3')
logger.setFilterMsg('filterkeyword') // 和小程序/小游戏端接口一致
logger.setFilterMsg('addfilterkeyword') // 和小程序/小游戏端接口一致

# How to view logs

Log in to We Analytics and go to the log query page from "Performance Quality - > Live Log."Developers can query log information for specified users by filtering criteria such as setting time, WeChat number / OpenID, page link, FilterMsg content (base library 2.7.3 and above supports setFilterMsg).If the real-time log is reported by the plug-in, you can enter the log query page from "Weixin Mini Program plug-in - > real-time log."

./log2.png

# Note

Due to background resource limitations, the rules for using "real-time logs" are as follows:

  1. For the convenience of locating the problem, logs are divided by page. A certain page is logged within a certain time (minimum of 5 seconds and maximum of the time interval between the page displayed and hidden).A log is reported, and the **log can be searched for in the Weixin Mini Program administration background based on the page path.
  2. Per Weixin Mini Program account, We Analytics Basic is limited to 5,000 logs per day, and We Analytics Professional is limited to 50,000 logs per day, and supports the purchase of configuration upgrades or the purchase of additional reporting extensions.Depending on the version configuration, the logs will remain for 7 days, 14 days, 30 days, and it is recommended that problems be resolved in a timely manner.
  3. The upper limit of a log is 5KB, including up to 200 times print log function calls (info, warn, error call count), so be careful to play the log, avoid calling the log interface in the loop, avoid direct rewrite console.log way to play the log.
  4. See the log inside the feedback, you can search the log according to OpenID.
  5. Set FilterMsg and addFilterMsg can set filter fields similar to log tags. If you need to add multiple closed keys, it is recommended to use addFilterMsg.For example, addFilterMsg ('scene1'), addFilterMsg ('scene 2'), addFilterMsg, ('scene3'), after setting in the Weixin Mini Program management background can be randomly combined three key words for retrieval, such as:"scene1scene2scene3," "scene1scene2," "scene1scene3" or "scene2," etc. (separated by spaces, so addFilterMsg cannot have spaces). All of the above search methods can retrieve the log, and the more search conditions the more accurate the search is.
  6. At present, in order to facilitate log analysis, plug-in side real-time log only supports key-value format.
  7. Real-time logging is currently only supported on mobile testing.The tool-side interface can be called but not reported to the background.
  8. Development version, experience version of the real-time log, do not count into the relevant quota, that is, there is no upper limit.

filtermsg