微信小程序操作日志上


  微信小程序操作日志記錄,如果是用戶量不多或者后台人手安排不足情況下,可以考慮使用微信官方的api上報日志。下面說說此方法的具體實現。
  先說下展示結果,查看日志的步驟:微信小程序管理后台mp.weixin.qq.com頁面的開發目錄=>開發管理=>運維中心=>實時日志。
  
代碼實現:
 

// 看項目指什么為唯一值界限,例如用戶userId或者用戶手機號等等,我這邊是以用戶手機號區分,居於uniapp所以使用了vuex獲取用戶信息,原生開發的可以使用getApp().globalData.XX || wx.getStorageSync("XX")
import $store from "@/store/index.js"; // 獲取小程序當前環境,用於關鍵字過濾操作
import { env } from "./base.js" class LogManager { constructor() { this._log = null;                // 日志管理器實際功能變量
        this.infoStorage = [];            // info級別日志管理
        this.warnStorage = [];            // warn級別日志管理
        this.errorStorage = [];            // error級別日志管理
 } // 初始化
 init() { // #ifdef MP-WEIXIN
            this._log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null; // #endif
 } // 實際上報操作
 saveLogFunc(args, type) { if (!this._log || !this._log[type]) return; let _mobile = $store.getters["baseInfo/userInfo"].mobile; // 先試用`${type}Storage`臨時緩存,如果用戶正常登錄,獲取到手機號碼的話,上報日志,否則等待下一次觸發
        this[`${type}Storage`].push(args); if(!!_mobile) { this.setFilterMsg(`${env}${_mobile}`) try{ this._log[type].apply(this._log, this[`${type}Storage`]) } catch( err) { return } this[`${type}Storage`] = []; } } // 操作過濾關鍵字
 handleFilterMsg(fn, type) { if (!this._log || !this._log[type]) return
        if (typeof msg !== 'string') return 
        try{ fn() } catch( err) { return } } // 寫 info 日志
 info() { this.saveLogFunc(arguments, "info") }; // 寫 warn 日志
 warn() { this.saveLogFunc(arguments, "warn") } // 寫 error 日志
 error() { this.saveLogFunc(arguments, "error") } // 設置過濾關鍵字,從基礎庫2.7.3開始支持
 setFilterMsg(msg) { this.handleFilterMsg(function() { this._log.setFilterMsg(msg) }.apply(this), "setFilterMsg") } // 多個過濾關鍵字,從基礎庫2.8.1開始支持
 addFilterMsg(msg) { this.handleFilterMsg(function() { this._log.addFilterMsg(msg) }.apply(this), "addFilterMsg") } } export default LogManager;

一般用戶反饋,我們可以獲取用戶手機號,或者使用微信小程序的客服功能的獲取到用戶id等等,這些就是我們查找用戶日志的有效數據。再加上時間段的過濾,例如我查看開發環境13242808961手機號碼用戶的日志

在小程序App 實例app.vue/app.js引入,上報你的操作數據吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM