3.2.3 • Published 5 years ago

miniprogram-logger v3.2.3

Weekly downloads
13
License
Apache-2.0
Repository
github
Last release
5 years ago

miniprogram-logger

It is used to logger and gather statistics of users' behavior by using wx.reportAnalytics

example

import { logger } from "miniprogram-logger";
logger.debug("action", "log something", correlation_id);
logger.info("action2", "info or content", correlation_id);
logger.warn("action2", "important messages", correlation_id);
logger.error("action2", { err: Exception }, correlation_id);

// 开始计时开始
logger.time("timer-label", { action: "", param: "" });
// 完成计时结束
logger.timeEnd("timer-label");

// 耗时统计一次写入
logger.timeLog("action", 1000, { param: "xxx" }, "request-ID", { errMsg: "" },"type");
logger.timeLog({
    action: string,
    duration: number,
    parameter: any,
    requestId: string,
    result: string | any,
    ...
});

// 用户行为统计
logger.telemetry("do something", { p: "参数" }, {});

// 全局参数设置
logger.setContext("key", "value");

API

Const object

  • logger 统一的默认 logger 对象,类似 console api 方式封装下面全部 (推荐使用)
  • defaultCounter 默认计数上报对象
  • defaultLogManager 本地日志管理对象
  • defaultLogReporter 自定义分析日志上报对象
  • defaultTimer 耗时统计上报对象
  • defaultTelemetry 用户行为统计上报对象

Function

  • inject() listen and log the app onError/onPageNotFound
  • guid() get a UUID like string
  • isLogLevel(level:any) level is LogLevel or not

Class

  • ConsoleManager
  • CounterReportMonitor
  • LogReporter
  • LogManager
  • LogObject
  • LogLevel
  • TelemetryReporter
  • TimeReporter
  • PerformanceObject

Install

npm i miniprogram-logger -S

1. Create the data event in the MiniProgram backend as follow:(Reference: )

https://developers.weixin.qq.com/miniprogram/analysis/custom/

  • log
  • telemetry
  • time

idrecord_time 默认会自动生成

下列所有非number类型均对应string

DefaultLogObject

日志对象

{
    "level": "LogLevel",
    "action": "string",
    "content": "any",
    "user": {},
    "correlation_id": "string",
    "id": "string",
    "timestamp": "string"
}

上报事件名log; 表结构

字段类型说明
idstring单条记录过滤 ID
levelstring日志级别
actionstring操作
contentstring内容
correlation_idstring关联 ID
userstring客户端脱敏数据
timestampstringISO 时间戳

TelemetryObject

记录 Telemetry

{
    "id": "string",
    "action": "string",
    "param": "any",
    "extension": {
        "debug_correlation_id": "string",
        "process_time": "number",
        "timestamp": "string"
    },
    "user": {
        "app_name": "string",
        "app_id": "string",
        "open_id": "string",
        "union_id": "string"
    }
}

自定上报默认事件名telemetry; 表结构

字段类型说明
idstring单条记录过滤 ID
actionstring操作
paramstring参数
extensionstring其它数据
userstring客户端脱敏数据

TimeReporter

时间统计对象

{
    "action": "string",
    "duration": "number",
    "param": "any",
    "correlation_id": "string",
    "result": "any",
    "type": "string",
    "user": "any",
    "id": "string",
    "timestamp": "string"
}

自定上报默认事件名time;

表结构

字段类型说明
idstring单条记录过滤 ID
actionstring操作
timenumber操作耗时
paramstring参数
correlation_idstring关联 ID
resultstring操作结果
typestring操作分类
userstring客户端脱敏数据
timestampstringISO 时间戳

App Error

import { inject } from "miniprogram-logger";
inject(); //自动记录全局错误

task flow

                                                     +------------+
                        +-----------------------+--->+  monitor   | 监控报警
                        |         count         |    +------------+
                        |                       |
               +--------+----------+            |    +------------+
               |                   |     Filter +---->            |
               |                   +----------------->  Console   | 命令行
  log/info/... |                   |                 |            |
+------------> |                   |                 +------------+
               |                   |                 +------------+
  telemetry    |                   |     Filter      |            |
+------------> |                   +---------------->+ LogManager | 本地文件
               |                   |                 |            |
 time/timeLog  |        logger     |                 +------------+
+------------> |                   |                 +------------+
               |                   |     Filter      |            |
               |                   +---------------->+  Reporter  | 微信后端
               |                   |                 |            |
               |                   |                 +------------+
               |                   |     Filter      |            |
               |                   +---------------->+ realTimeLog| 实时日志
               |                   |                 |            |
               +-------------------+                 +------------+
3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago