tencent cloud

HarmonyOS SDK Log Upload
Last updated: 2025-11-07 17:40:38
HarmonyOS SDK Log Upload
Last updated: 2025-11-07 17:40:38
This article introduces how to quickly get started with the HarmonyOS SDK of Cloud Log Service (cls) to implement log upload. For more details on SDK usage, refer to the code repository: tencentcloud-cls-SDK-ohos.

Prerequisites

Installed the HarmonyOS application development environment. For more information, please refer to HarmonyOS Developer Guide.
Create and obtain TencentCloud API key information access_secret and access_key. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.

Installing the HarmonyOS SDK

1. Create a HarmonyOS application project.
2. Import the CLS HarmonyOS module and run the following command under the project.
ohpm install @farmerx/tencntcloud-cls-sdk-ohos
3. After execution, you can import the CLS module in the specified ets file for reference and write code.
import { TencntcloudLog, LogCallback } from "@farmerx/tencntcloud-cls-sdk-ohos"

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Regional information. For completion, see the domain name in the API upload log Tab under available region.
access_secret
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
access_key
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
access_token
String
No
Temporary key token. For example, use temporary key to auto-fill.
topic_id
String
Yes
Log topic ID information.

Upload Example Code for Logs

In your HarmonyOS project, you can use the following example code to implement log upload. The sample code is as follows.
Using cloud API key (access_key, access_secret) in code as plaintext carries high risk. To ensure security, recommend using temporary key to authenticate.
import {LogProducer} from "@farmerx/tencntcloud-cls-sdk-ohos";

// Initialize
let producer = new LogProducer(
"#[topic_id]",
"#[access_key]",
"#[access_secret]",
"#[access_token]",
"",
"#[endpoint]", 100, 100, 2000)

// Set the callback function
producer.setCallback((err: Error | null, topicID: string, code: number, request_id: string, errorMessage: string) => {
if (err==null) {
hilog.info(0x0000,
'testTagCLS',
'onLogCallback.topic: %{public}s , code: %{public}d, request_id: %{public}s, errorMessage: %{public}s',
topicID, code, request_id, errorMessage
);
} else {
hilog.info(0x0000,
'testTagCLS',
'onLogCallback.topic: %{public}s, code: %{public}d, request_id: %{public}s, errorMessage: %{public}s',
topicID, code, request_id, err.message
);
}
})

// Set the source IP
producer.setSourceIp("127.0.0.3")


// reset access key
producer.setAccessKey("#[access_key]", "#[access_secret]", "#[access_token]");

// Synchronous sending
function syncsencLog() {
let logs: Record<string, string>[] = [];
let log: Record<string, string> = {};
// Add key-value pair
log["key1"] = "value1";
log["key2"] = "value2";
log["key3"] = "value3";
logs.push(log)
try {
let rsp = producer.syncSendLogs( "127.0.0.1", logs)
hilog.info(0x0000, 'testTag', "========%{public}s, %{public}s", rsp.requestId, rsp.rspText)
} catch (e) {
// ....
}
}
// Async send
function asyncSendLog() {
let log: Record<string, string> = {};
// Add key-value pair
log["key1"] = "value1";
log["key2"] = "value2";
log["key3"] = "value3";
try {
producer.addLog(log)
} catch (e) {
// ....
}
}

asyncSendLog()
sencLog()


Conclusions

Through these steps, you can use Tencent Cloud CLS HarmonyOS SDK to quickly upload logs. If you encounter any issues, contact us to get help.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback