HarmonyOS NEXT client diagnosis

更新时间:
复制 MD 格式

This document explains how to write client diagnostic logs and trigger their upload on HarmonyOS NEXT.

Use the client diagnosis API

Add the @mpaas/trace_log dependency to your project. For the required version, see the Integrate mPaaS capabilities guide.

The following code shows how to use the API:

import {createLogger} from '@mpaas/trace_log';

export default class MPLog {

  private static DOMAIN : number = 0x0011;
  private static TAG: string = 'BizCustomTag';

  static info(msg: string): void {
    const logger = createLogger();
    logger.info(MPLog.DOMAIN, MPLog.TAG, msg);
  }

  static warn(msg: string): void {
    const logger = createLogger();
    logger.warn(MPLog.DOMAIN, MPLog.TAG, msg);
  }

  static error(msg: string, error?: Error): void {
    const logger = createLogger();
    if (!error) {
      logger.error(MPLog.DOMAIN, MPLog.TAG, msg);
    } else {
      logger.error(MPLog.DOMAIN, MPLog.TAG, msg + ' ' + error.message + ' ' + error.stack ?? '');
    }
  }
}

Integrate client diagnosis

You can trigger diagnosis log uploads by using push messages from the server. To do this, integrate the Message Push service. When the Push SDK receives an upload command, it uploads the diagnosis logs. See the Message Push SDK guide for HarmonyOS NEXT to configure the passthrough message Ability.

After you integrate the passthrough message Ability, the client diagnosis setup for the message push channel is complete.

Pull logs from the console

The mPaaS console lets you pull logs to quickly analyze crashes or exceptions on a specific device or for a particular user.

Step 1: Create a log-pull task

  1. Log on to the mPaaS console and select your target app.

  2. In the left navigation pane, choose Mobile Analysis > Log Management.

  3. On the Pull Real-time Logs tab, click Add.

  4. Fill in the task details. The user ID is the unique identifier for a logged-in user. You set this ID by using the user ID upload method in the Message Push service.

  5. Click Confirm to create the log-pull task.

Step 2: Trigger the log-pull task

  1. In the list of log-pull tasks, find the task you just created, select a Trigger Channel, and then click Trigger in the Actions column.

  2. Wait a moment and refresh the page. Check the task status:

    • Task Completed: Click View to download the diagnosis logs.

    • Push Call Successful: This status indicates that the upload command has been sent, but the client has not yet received it or uploaded the logs. If this status persists, ensure your app is running. If not, restart it.