Skip to content

Mini Program Log Collection


Mini programs actively send log data of different levels (corresponding source:browser_log metric type log data) to Guance.

Features

  • Custom log data collection, integrated into client applications via SDK, collecting different log data for different scenarios.
  • Automatically collects error information from the application end (including network errors, console errors, and js errors) and reports it to DataFlux.
  • Custom error levels (debug, critical, error, info, warn), custom Logger objects, and custom Log fields.
  • Automatically collects RUM related data, associating with RUM business scenarios (requires rum,sdk to be updated to the latest version).

Getting Started

Prerequisites

  • Introduce SDK: The SDK can be introduced into the application via NPM or CDN. After initialization, it can be stored in global variables for easy reference by other pages.
  • Supported Mini Program Clients: WeChat, Baidu, Alipay, Toutiao, and most other mini program clients.

Access Methods

  1. Ensure DataKit is installed and configured to be publicly accessible, and install the IP geolocation library.
  2. Obtain parameters such as applicationId, env, version from the console, and start accessing the application.
  3. When integrating the SDK, set datakitOrigin to the domain name or IP of DataKit.

  1. Obtain parameters such as applicationId, clientToken, and site from the console, and start accessing the application.
  2. No need to configure datakitOrigin when integrating the SDK. Data will be sent to the public DataWay by default.

NPM Introduction

//#ifndef H5 || APP-PLUS || APP-NVUE || APP-PLUS-NVUE
const { datafluxLogs } = require('@cloudcare/dataflux-rum-miniapp-logs')
// Initialize Rum
datafluxLogs.init({
  datakitOrigin: '<DATAKIT ORIGIN>'
  site: "http://172.16.212.186:9529", // Domain name of the site corresponding to the public DataWay
  clientToken: "a993f53a8ea04bc6b9350e5e670a3a3b", // Client token required for public DataWay reporting
  service: 'minapp',
  env: 'prod',
  version: '1.0.0'
})
//#endif
//#ifndef H5 || APP-PLUS || APP-NVUE || APP-PLUS-NVUE
const { datafluxLogs } = require('@cloudcare/dataflux-rum-miniapp-logs')
// Initialize Rum
datafluxLogs.init({
    datakitOrigin: '<DATAKIT ORIGIN>'
  service: 'miniapp',
  env: 'prod',
  version: '1.0.0'
})
//#endif

Configuration

Initialization Parameters

Parameter Type Required Default Description
datakitOrigin String Yes DataKit data reporting Origin Note: protocol (including ://), domain name (or IP address) [and port number]
For example: https://www.datakit.com, http://100.20.34.3:8088
clientToken String Yes Token for reporting data via openway, obtained from the Guance console, required (for public openway access).
site String Yes Address for reporting data via public openway, obtained from the Guance console, required (for public openway access).
service String No browser Log Service name
env String No Current environment of the Web application, e.g., Prod: production environment; Gray: grayscale environment; Pre: pre-release environment Common: daily environment; Local: local environment;
version String No Version number of the Web application
sampleRate Number No 100 Metric data collection percentage: 100 means full collection, 0 means no collection
forwardErrorsToLogs Boolean No true Set to false to stop collecting console.error, js, and network errors reported to DataFlux log data

Usage

After the SDK is initialized in the application, log data can be custom configured through the exposed SDK API.

logger.debug | info | warn | error | critical (message: string, messageContext = Context)

NPM

import { datafluxLogs } from "@cloudcare/dataflux-rum-miniapp-logs"

datafluxLogs.logger.info("Button clicked", { name: "buttonName", id: 123 })

Return Data Structure

{
  "service": "miniapp",
  "session": {
    "id": "c549c2b8-4955-4f74-b7f8-a5f42fc6e79b"
  },
  "type": "logger",
  "_dd": {
    "sdk_name": "miniapp LOG SDK",
    "sdk_version": "1.0.0",
    "env": "",
    "version": ""
  },
  "device": {
    "platform_version": "8.0.5",
    "model": "iPhone 6/7/8 Plus",
    "app_framework_version": "2.23.3",
    "os": "OS",
    "os_version": "10.14.6",
    "os_version_major": "10",
    "screen_size": "414*736",
    "network_type": "3g",
    "divice": "devtools"
  },
  "user": {},
  "date": 1621321916756,
  "view": {
    "referrer": "",
    "name": "pages/index/index",
    "id": "5dce64f4-8d6d-411a-af84-c41653ccd94a"
  },
  "application": {
    "id": "app_idxxxxxx"
  },
  "message": "XHR error get http://testing-ft2x-api.cloudcare.cn/api/v1/workspace/xxx",
  "status": "error",
  "error": {
    "source": "network",
    "stack": "Failed to load"
  },
  "resource": {
    "method": "get",
    "status": 0,
    "status_group": 0,
    "url": "http://testing-ft2x-api.cloudcare.cn/api/v1/workspace/xxx",
    "url_host": "testing-ft2x-api.cloudcare.cn",
    "url_path": "/api/v1/workspace/xxx",
    "url_path_group": "/api/?/workspace/xxx"
  }
}

Status Parameter

After initializing the SDK, you can use the provided log API to define different types of statuses.

log (message: string, messageContext: Context, status? = 'debug' | 'info' | 'warning' | 'error' | 'critical')

Usage

import { datafluxLogs } from '@cloudcare/dataflux-rum-miniapp-logs'

datafluxLogs.logger.log(<MESSAGE>,<JSON_ATTRIBUTES>,<STATUS>);

Parameter Description

Parameter Description
<MESSAGE> The Message field in Dataflux logs.
<JSON_ATTRIBUTES> Additional data describing the Message, a Json object.
<STATUS> Log level, optional values: debug, info, warning, error, critical.

Feedback

Is this page helpful? ×