全部产品
Search
文档中心

Simple Log Service:Ikhtisar Simple Log Service SDK untuk HarmonyOS

更新时间:Jul 02, 2025

Gunakan Simple Log Service SDK untuk HarmonyOS untuk mengumpulkan log dari perangkat HarmonyOS. SDK ini mengenkapsulasi operasi API terkait pengumpulan Layanan Log Sederhana berdasarkan ArkTS dan ditulis dalam bahasa pemrograman C.

Versi

Simple Log Service SDK untuk HarmonyOS dihosting dan dirilis melalui OpenHarmony Package Management. Untuk informasi lebih lanjut, lihat Aliyun Log HarmonyOS SDK Release.

Kode contoh

Simple Log Service SDK untuk HarmonyOS menyediakan berbagai kode contoh yang dapat digunakan atau direferensikan.

import hilog from '@ohos.hilog';

// Impor modul SDK.
import { AliyunLog, LogCallback } from "@aliyunsls/producer"

// Inisialisasi SDK.
let aliyunLog: AliyunLog = new AliyunLog(
  "<your endpoint>",
  "<your project>",
  "<your logstore>",
  "<your accesskey id>",
  "<your accesskey secret>",
  "<your accesskey token>" // Variabel ini hanya diperlukan jika pasangan AccessKey diperoleh dengan memanggil operasi Security Token Service (STS).
);

// Tulis log.
aliyunLog.addLog(new Map(
  [
    ["key1", "value1"],
    ["key2", "value2"],
    ["key3", "value3"],
  ]
));

class MyLog implements LogCallback {

  // Konfigurasikan callback log.
  init() {
    aliyunLog.setLogCallback(this);
  }

  // Tulis log.
  addLog() {
    aliyunLog.addLog(new Map([
      ["key1", "val1"],
      ["key2", "val2"],
      ["key3", "val3"],
      ["key4", "val4"],
    ]))
  }

  // Proses callback log.
  // Untuk informasi tentang nilai valid dari code, lihat topik terkait.
  onLogCallback(logStore: string, code: number, logBytes: number, compressedBytes: number, errorMessage: string) {
    hilog.info(0x0000,
      'testTag',
      'onLogCallback. logStore: %{public}s, code: %{public}d, logBytes: %{public}d, compressedBytes: %{public}d, errorMessage: %{public}s',
      logStore, code, logBytes, compressedBytes, errorMessage
    );
    aliyunLog.setAccessKey(
      "<your accesskey id>", // Tentukan ID AccessKey yang valid.
      "<your accesskey secret>", // Tentukan Rahasia AccessKey yang valid.
      null // Tentukan token AccessKey yang valid. Variabel ini hanya diperlukan jika pasangan AccessKey diperoleh dengan memanggil operasi STS.
    );
  }
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  private log: MyLog = new MyLog();

  build() {
    Row() {
      Column() {
        Text("Init")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.log.init();
          });
        Text("Add")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.log.addLog();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Penagihan

Biaya yang timbul saat menggunakan Simple Log Service SDK untuk HarmonyOS sama dengan biaya yang berlaku saat mengumpulkan log di konsol Simple Log Service. Untuk informasi lebih lanjut, lihat Ikhtisar Penagihan.

Referensi