LTS HarmonyOS SDK
This SDK provides a series of methods for reporting logs in ArkTS, enabling you to directly report logs to LTS in encoding mode.

This function is available for whitelisted users in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. To use it, create a service ticket.
Transport Protocol
HTTPS
Prerequisites
- You have obtained the name of the region where LTS is deployed.
- You have obtained a project ID of your Huawei Cloud account. For details, see API Credentials.
- You have obtained the IDs of the target log group and log stream in LTS.
Version Updates
Version |
Download Address |
Description |
System |
---|---|---|---|
0.0.3 |
Moved the log writing and reporting logic to sub-threads, and added code obfuscation and unit test cases. |
HarmonyOS NEXT or later |
|
0.0.2 |
Stord the log status in the database. |
HarmonyOS NEXT or later |
|
0.0.1 |
Launched HarmonyOS SDK, which supports cache reporting and immediate reporting. |
HarmonyOS NEXT or later |
Installing the HarmonyOS SDK
- Integrate the SDK. The SDK package is in ArkTS and can be imported only to ArkTS files.
- Integration through the HarmonyOS repository:
- Run this command in your terminal:
ohpm install lts-harmony-sdk
- Add dependencies to the app/oh-package.json5 file.
dependencies { ... "lts-harmony-sdk": "0.0.3" ... }
- Enable the SDK.
ohpm install
- Run this command in your terminal:
- Manual integration:
- Download the LTS SDK package.
- Decompress the downloaded package to a specified directory. Note: Decompress the package only.
- Add the decompressed HAR static library file to your project.
- Add dependencies to the app/oh-package.json5 file.
dependencies { ... "lts-harmony-sdk": "file:../plugin/LTS_SDK/default/lts-harmony-sdk.har" // Path of the HAR package ... }
- Enable the SDK.
ohpm install
- Integration through the HarmonyOS repository:
- Perform initialization. For details about the parameters, see Table 4.
The LTS SDK supports multi-instance log reporting. Instances are created based on different configurations for reporting.
- Add dependencies.
import { LTSSDK, ConfigParam } from 'lts-harmony-sdk';
- ArkTS: example initialization code
// LTS parameter configuration const config:ConfigParam = { // Mandatory parameters region: string, // Region where the logs are reported projectId: string, // Huawei Cloud project ID groupId: string, // LTS log group ID streamId: string, // LTS log stream ID accessKey: string, // Huawei Cloud AK secretKey: string, // Huawei Cloud SK // Optional parameters url: string, // Address of log reporting cacheThreshold: number, // Threshold for the number of reported log records timeInterval: number, // Threshold for the interval between log reports isReportBackground: boolean // Whether to report logs when running in the background }; // Obtain ApplicationContext. let applicationContext = this.context.getApplicationContext(); // LTS initialization method const ltssdk = new LTSSDK(applicationContext, config)
- Add dependencies.
- Report logs. LTS provides two log reporting methods.
Table 2 Log reporting methods Method
Description
report(content, labels)
LTS stores logs in the local database and reports them in real time based on the configured policy.
reportImmediately(content, labels)
Logs are reported immediately.
Table 3 Parameters Name
Description
content
Log content. The value can be a dictionary or a dictionary array. A maximum of 300 key-value pairs are supported. The maximum length of the JSON string converted from content is 30 × 1024. If the length exceeds the limit, the excess part will be truncated.
labels
Log labels. The value can be a dictionary or null. A maximum of 50 outermost key-value pairs are allowed. The maximum length of an outermost key is 64 characters. The key can contain letters, digits, and underscores (_), and must start with a letter. The maximum length of a dictionary converted to a JSON string is 30,720 characters. If the length exceeds the maximum, the excess part will be truncated before being reported.
ArkTS code example:
const ltssdk = new LTSSDK(applicationContext, config) let fruit = { "fruit_1": "apple", "fruit_2": "pear", "fruit_3": "banana" }; let food = { "food_1": "rice" }; let labels = { "date":"2023-10-01"}; let contents = [food, fruit]; ltssdk.report(food); // Cache and report a single record, without labels ltssdk.report(food, labels); // Cache and report a single record, with labels ltssdk.reportImmediately(food); // Immediately report a single record, without labels ltssdk.reportImmediately(food, labels); // Immediately report a single record, with labels ltssdk.report(contents); // Cache and report multiple records, without labels ltssdk.report(contents, labels); // Cache and report multiple records, with labels ltssdk.reportImmediately(contents); // Immediately report multiple records, without labels ltssdk.reportImmediately(contents, labels); // Immediately report multiple records, with labels
Parameters
Parameter |
Type |
Mandatory |
Default Value |
Description |
---|---|---|---|---|
projectId |
String |
Yes |
- |
Project ID of the Huawei Cloud account. |
groupId |
String |
Yes |
- |
LTS log group ID. |
streamId |
String |
Yes |
- |
LTS log stream ID. |
accessKey |
String |
Yes |
- |
AK of the Huawei Cloud account. Hard-coded or plaintext AK and SK of the Huawei Cloud account for authentication are risky. For security, encrypt them. Decrypt them only when necessary. |
secretKey |
String |
Yes |
- |
SK of the Huawei Cloud account. Hard-coded or plaintext AK and SK of the Huawei Cloud account for authentication are risky. For security, encrypt them. Decrypt them only when necessary. |
region |
String |
Yes |
- |
Region where LTS is deployed. |
url |
String |
No |
null |
Public network domain name for reporting. If it is not set, a link is automatically generated based on the region. The format is as follows: https://lts-access.{region}.myhuaweicloud.com |
cacheThreshold |
number |
No |
200 |
When the number of logs stored in the local database reaches this threshold, an alarm is reported. Value range: 30 to 1000. |
timeInterval |
number |
No |
3 seconds |
Interval for triggering reporting. Value range: 1 to 1800, in seconds. |
isReportBackground |
Boolean |
No |
true |
Whether to report logs when the app switches to running in the background. |
Log Reporting Policies
The LTS SDK supports the following log reporting policies: threshold-based reporting, scheduled reporting, and reporting when the app is switched to the background. Set parameters such as cacheThreshold, timeInterval, and isReportBackground based on the scenario.
Configuring Permissions
The LTS SDK requires the network permission of HarmonyOS. If this permission is not configured, add the following content to the module.json5 file of the app:
{ "module": { ... "requestPermissions":[ ... { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.GET_NETWORK_INFO" } ], ... }
Debugging
Enable debugging if needed. Then you can monitor log reporting in real time on the console, and make adjustments accordingly.
Call the static method of the LTS SDK to make adjustments. The SDK supports five log levels: Debug, Info, Warning, Error, and Off. The default log level is Off. The five levels correspond to the following parameters: LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARNING, LogLevel.ERROR, and LogLevel.OFF. The following is a code example:
ltssdk.setLogLevel(LogLevel.DEBUG)
Obtaining Parameters
- Read the region table.
Table 5 Regions Name
Region
CN North-Beijing4
cn-north-4
CN East-Shanghai1
cn-east-3
CN South-Guangzhou
cn-south-1
- To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
- To obtain a log stream's ID, click the down arrow on the left of the corresponding log group name to expand the log stream list, then hover over the target log stream's name.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot