# miniProgramLogger

基础类的小程序监控，适用于钉钉、支付宝、微信各类小程序

**参考文档**
> [eapp](https://open-doc.dingtalk.com/microapp/dev/framework-app)
> [微信小程序开发](https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/app.html)

## 1.开始使用

```ts
    /**
     * @desc 监控sdk初始化,debug模式仅打印日志，不发送日志
     * 以钉钉中的用法为例
     * monitor.js
     */
    import MiniProgramLogger from 'alife-logger/miniprogram';
    module.exports = EAppLogger.singleton({
        debug: true, // 本地开发不发送日志，但打印日志
        pid: 'your-project-id',
        // 基础小程序监控需要手动传入rpc
        sendRequest: (url) => {
            dd.httpRequest({
                url,
                method: 'GET'
            });
        },
        // 需要手动传入获取当前页面路径的方法
        getCurrentPage: () => {
            if (typeof getCurrentPages !== 'undefined' && typeof getCurrentPages === 'function') {
                var pages = (getCurrentPages() || []);
                var pageLength = pages.length;
                var currPage = pages[pageLength - 1];
                return (currPage && currPage.route) || null;
            }
        }
    });
    /**
     * 启动页 app.js
     * @see E应用 https://open-doc.dingtalk.com/microapp/dev/framework-app
     * @see 微信 https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/app.html
     */
    import logger from './monitor';
    App(logger.hookApp({
        onLaunch(options) {
            /**
             * @desc 发送launch打点t=appLaunch
             */
            logger.appLaunch();
        },
        onShow(options) {
            /**
             * @desc 发送shw打点 t=appShow
             */
            logger.appShow();
        },
        onError(msg) {
            /**
             * @desc 发生 js 错误时触发
             * @function 打点jserror
             * E应用和微信小程序的onError，参数都为msg: string,错误触发为脚本或者api调用失败
             */
            logger.error(msg)
        },
    }));
    /**
     * page页
     * @see E应用 https://open-doc.dingtalk.com/microapp/dev/framework-page
     * @see 微信 https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html
     */
    import logger from './monitor';
    Page(logger.hookPage({
        data: {},
        /**
         * @see E应用 https://open-doc.dingtalk.com/microapp/dev/httprequest
         * @see 微信 https://developers.weixin.qq.com/miniprogram/dev/framework/ability/mDNS.html
         */
        onShow() {
            // 页面显示
            /**
             * t=pv
             */
            logger.pageShow();
        },
        onHide() {
            /**
             * @desc 发送health数据。t=health
             */
            logger.pageHide();
        },
        onUnload() {
            /**
             * @desc 发送health数据。t=health
             */
            logger.pageHide();
        },
        onLoad(query) {
            const apiUrl = 'http://httpbin.org/post';
            const startTime = Date.now();
            // 页面加载
            dd.httpRequest({
                url: apiUrl,
                method: 'POST',
                data: {},
                success: function(res) {
                    const endTime = Date.now();
                    /**
                     * t=api
                     * @desc 发送api日志
                     */
                    logger.api({
                        api: apiUrl,
                        success: true,
                        time: endTime - startTime,
                        code: res.status,
                    });
                },
                fail: function(res) {
                    const endTime = Date.now();
                     /**
                     * t=api
                     * @desc 发送api日志
                     */
                    logger.api({
                        api: apiUrl,
                        success: false,
                        time: endTime - startTime,
                        code: res.status,
                    });
                },
                complete: function(res) {}
            });
        },
        onTitleClick() {
            /**
             * 统计打点数据
             * @desc
             */
            logger.sum('titleClick');
        },       
    }));
```

## 2. 通用API

api示例请参考 **Getting Started**

| 方法  | 参数 |  备注      | 使用场景举例 | 
| -------- | -------- | -------- | -------- |
| setCommonInfo  | {[key: string]: string;}  | 设置日志基础字段 | 灰度 |
| appLaunch  | {}  | app launch打点 |  |
| appShow  | {}  | app show打点 |  |
| pageShow  | {}  | page show打点，发送pv数据 |  |
| pageHide  | {}  | page hide打点，发送health数据 |  |
| error  | string|object  | 错误日志打点 |  |
| hookApp  | {}  | 请传入原有的App参数 | 自动在App的生命周期中打点 |
| hookPage  | {}  | 请传入原有的Page参数 | 自动在Page的生命周期中打点 |

大部分日志上报api见上面的示例即可

## 3. 说明
> 小程序系列监控的项目要使用hookApp、hookPage嵌入生命周期打点，必须符合标准小程序规范: App、Page。
即App层有 onLaunch、onShow、onError;
Page层有 onShow、onHide、onUnload。

## 4. 日志上报API

> 请参见 [README](README.md) 的 `API接口 > 数据上报接口`
