WeChat applet realizes the access of real-time log and interface monitoring

Premise: The product proposal can monitor the entire process of dish display, additional purchase, and order placement in real time. It is convenient for the official environment to view the operation status of the interface. So it is connected, the log and interface monitoring of the applet.

Log.js

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)
    },
    // 实时日志
    // 业务监控---基础监控--接口监控
    joggleMonitor(monitorId, errorCode, errorMsg, time) {
        wx.reportEvent && wx.reportEvent('wxdata_perf_monitor', {
            // 接口ID,可以使用URL相似的ID,也可以另外命名
            "wxdata_perf_monitor_id": monitorId,
            //接口等级,0为普通,非0为重要,数值越大越重要,根据实际业务情况进行设置
            "wxdata_perf_monitor_level": 1,
            // 错误码,0为调用成功,非0为调用失败
            "wxdata_perf_error_code": errorCode,
            // 错误信息,选填,可以上报错误相关信息,方便后续排查问题
            "wxdata_perf_error_msg": errorMsg,
            // 接口耗时,选填
            "wxdata_perf_cost_time": time,
            // 以下为补充字段,可额外上报其他监控信息,用于事件分析,非必填
            "wxdata_perf_extra_info1": "",
            "wxdata_perf_extra_info2": "",
            "wxdata_perf_extra_info3": "",
        })
    },
    // 业务监控---基础监控--业务逻辑监控
    businessMonitor(monitorId, errorCode, errorMsg, time, content) {
        // content==业务逻辑内容
        wx.reportEvent && wx.reportEvent('wxdata_perf_monitor', {
            // 接口ID,可以使用URL相似的ID,也可以另外命名
            "wxdata_perf_monitor_id": monitorId,
            //接口等级,0为普通,非0为重要,数值越大越重要,根据实际业务情况进行设置
            "wxdata_perf_monitor_level": 1,
            // 错误码,0为调用成功,非0为调用失败
            "wxdata_perf_error_code": errorCode,
            // 错误信息,选填,可以上报错误相关信息,方便后续排查问题
            "wxdata_perf_error_msg": errorMsg,
            // 接口耗时,选填
            "wxdata_perf_cost_time": time,
            // 以下为补充字段,可额外上报其他监控信息,用于事件分析,非必填
            "wxdata_perf_extra_info1": "",
            "wxdata_perf_extra_info2": "",
            "wxdata_perf_extra_info3": "",
        })
    },
    // 功能/模块监控--搜索模块下的多接口监控
    functionMonitor(monitorId, moduleId, errorCode, errorMsg, time) {
        console.log("errorCode",errorCode, time)
        wx.reportEvent && wx.reportEvent('wxdata_perf_module_monitor', {
            // 接口ID,可以使用URL相似的ID,也可以另外命名
            "wxdata_perf_monitor_id": monitorId,
            "wxdata_perf_module_id": moduleId,
            //接口等级,0为普通,非0为重要,数值越大越重要,根据实际业务情况进行设置
            "wxdata_perf_monitor_level": 1,
            // 错误码,0为调用成功,非0为调用失败
            "wxdata_perf_error_code": errorCode,
            // 错误信息,选填,可以上报错误相关信息,方便后续排查问题
            "wxdata_perf_error_msg": errorMsg,
            // 接口耗时,选填
            "wxdata_perf_cost_time": time,
            // 以下为补充字段,可额外上报其他监控信息,用于事件分析,非必填
            "wxdata_perf_extra_info1": "",
            "wxdata_perf_extra_info2": "",
            "wxdata_perf_extra_info3": "",
        })
    },
    // 服务/流程监控
    serveStartMonitor(serviceId, stepId, monitorId, errorCode, errorMsg, time) {
        wx.reportEvent && wx.reportEvent('wxdata_perf_service_monitor', {
            "wxdata_perf_service_id": serviceId,
            "wxdata_perf_step_type": 1,
            "wxdata_perf_step_id": stepId,
            // 接口ID,可以使用URL相似的ID,也可以另外命名
            "wxdata_perf_monitor_id": monitorId,
            //接口等级,0为普通,非0为重要,数值越大越重要,根据实际业务情况进行设置
            "wxdata_perf_monitor_level": 1,
            // 错误码,0为调用成功,非0为调用失败
            "wxdata_perf_error_code": errorCode,
            // 错误信息,选填,可以上报错误相关信息,方便后续排查问题
            "wxdata_perf_error_msg": errorMsg,
            // 接口耗时,选填
            "wxdata_perf_cost_time": time,
            // 以下为补充字段,可额外上报其他监控信息,用于事件分析,非必填
            "wxdata_perf_extra_info1": "",
            "wxdata_perf_extra_info2": "",
            "wxdata_perf_extra_info3": "",
        })
    },
    serveEndMonitor(serviceId, stepId, monitorId, errorCode, errorMsg, time) {
        wx.reportEvent && wx.reportEvent('wxdata_perf_service_monitor', {
            "wxdata_perf_service_id": serviceId,
            "wxdata_perf_step_type": 0,
            "wxdata_perf_step_id": stepId,
            // 接口ID,可以使用URL相似的ID,也可以另外命名
            "wxdata_perf_monitor_id": monitorId,
            //接口等级,0为普通,非0为重要,数值越大越重要,根据实际业务情况进行设置
            "wxdata_perf_monitor_level": 1,
            // 错误码,0为调用成功,非0为调用失败
            "wxdata_perf_error_code": errorCode,
            // 错误信息,选填,可以上报错误相关信息,方便后续排查问题
            "wxdata_perf_error_msg": errorMsg,
            // 接口耗时,选填
            "wxdata_perf_cost_time": time,
            // 以下为补充字段,可额外上报其他监控信息,用于事件分析,非必填
            "wxdata_perf_extra_info1": "",
            "wxdata_perf_extra_info2": "",
            "wxdata_perf_extra_info3": "",
        })
    }
}

Call on the page that needs to be monitored

import Log from '../../../utils/log'
import moment from 'moment'
const start = +new Date()

//接口调用时使用
// 接口监控
dishLis_v5({}).then(res => {
    // Log.joggleMonitor('dishLis_v5',res.success ? 0 : 1, res.msg, +new Date - start)
    //  Log.functionMonitor('dishLis_v5','food',res.success ? 0 : 1, res.msg, +new Date - start)
    Log.serveEndMonitor('dishLis_v5_service','dishLis_v5_step'
}).catch(error => {
  Log.info("=====", moment(new Date()).format('YYYY-MM-DD hh:mm:ss'), "======")
  Log.error("报错", error)
})

Note: The output of the Log must be recorded when it is run on a mobile phone, and there is no record when it is run on a developer

View location: WeChat public platform--Development management--Operation and maintenance center--Web analysis--Performance quality--Real-time log

 

Guess you like

Origin blog.csdn.net/qq_41687299/article/details/130129435