Collect log data from HarmonyOS devices by using the Simple Log Service HarmonyOS SDK.
Prerequisites
The Simple Log Service HarmonyOS SDK is installed. For more information, see Install the HarmonyOS SDK.
Initialize the SDK
Use the following code to initialize the SDK.
import { AliyunLog, LogCallback } from "@aliyunsls/producer"
let aliyunLog: AliyunLog = new AliyunLog(
"<your endpoint>",
"<your project>",
"<your logstore>",
"<your accesskey id>",
"<your accesskey secret>",
"<your accesskey token>" // Required only when the AccessKey is obtained through STS.
);
The following table describes the parameters.
|
Variable |
Description |
Example |
|
endpoint |
The endpoint is the domain name for accessing Simple Log Service. The endpoint depends on the region of the project. For more information, see Endpoints. |
cn-hangzhou.log.aliyuncs.com |
|
project |
Project name: A project is a resource management unit in Simple Log Service. It is the main boundary for user isolation and access control. For more information, see Manage projects. |
test-project |
|
logstore |
Logstore name: A Logstore is the unit in Simple Log Service for collecting, storing, and querying log data. For more information, see Manage Logstores. |
test-logstore |
|
accesskey id |
The AccessKey ID is used to identify a user. For more information, see AccessKey pair. Follow the principle of least privilege and grant only the necessary permissions to RAM users. For more information about how to grant permissions, see Create a RAM user and grant permissions and Examples of custom RAM authorization. |
None |
|
accesskey secret |
The AccessKey secret is a key used to encrypt signature strings. Simple Log Service uses this key to verify the signature strings. Keep your AccessKey secret confidential. |
None |
|
accesskey token |
The AccessKey token. This is required only when the AccessKey is obtained through STS. |
Report logs
Use the addLog method to report custom business logs.
let code = aliyunLog.addLog(new Map(
[
["key1", "value1"],
["key2", "value2"],
["key3", "value3"],
]
));
A log is reported successfully only when code == 0. For more information about other status codes, see Status codes.
Configure an AccessKey
The SDK supports dynamic AccessKey updates. Use the following code to update the AccessKey:
aliyunLog.setAccessKey(
"<your accesskey id>", // Enter the correct AccessKey ID.
"<your accesskey secret>", // Enter the correct AccessKey secret.
null // Enter the correct AccessKey token. This is required only when the AccessKey is obtained through STS.
);
Set a log sending callback
You can set a log sending callback to receive status notifications for each log delivery attempt. Use the callback to monitor SDK operations or update parameters.
class MyLogCallback implements LogCallback {
init() {
aliyunLog.setLogCallback(this);
}
onLogCallback(logStore:string, code:number, logBytes:number, compressedBytes:number, errorMessage:string) {
// An error code of 6 indicates that the AccessKey is invalid.
// An error code of 11 indicates a parameter configuration error.
if (code == 6 || code == 11) {
aliyunLog.setAccessKey(
"<your accesskey id>", // Enter the correct AccessKey ID.
"<your accesskey secret>", // Enter the correct AccessKey secret.
null // Enter the correct AccessKey token. This is required only when the AccessKey is obtained through STS.
);
}
}
}
Status codes
|
Status code |
Description |
Solution |
|
0 |
Success |
Normal. No action is required. |
|
1 |
The SDK is destroyed or invalid. |
Check whether the SDK initialization failed. |
|
2 |
Data write error. |
The project has reached its write traffic limit. If you require a higher limit, submit a ticket to contact technical support. |
|
3 |
The cache is full. |
submit a ticket to contact technical support. |
|
4 |
Network error. |
Check your network connectivity and retry the operation. |
|
5 |
The project write quota is full. |
The project has reached its write traffic limit. If you require a higher limit, submit a ticket to contact technical support. |
|
6 |
The AccessKey is expired or invalid. |
The AccessKey is expired or invalid, or the access policy of the account to which the AccessKey belongs is incorrect. Check the access policy of the RAM user. The RAM user must have permissions to perform operations on Simple Log Service resources. For more information, see Grant permissions to a RAM user. |
|
7 |
Service error. |
A service fault occurred. submit a ticket to contact technical support. |
|
8 |
Data is discarded. |
This error usually occurs because the device time is not synchronized with the server time. The SDK automatically resends the data. |
|
9 |
The device time is not synchronized with the server time. |
The device time is not synchronized with the server time. The SDK automatically resolves this issue. |
|
10 |
Cached data was not sent when the SDK was destroyed. |
This may cause data loss. You can enable the resumable upload feature to prevent data loss. |
|
11 |
SDK initialization parameter error. |
This error usually occurs because the AccessKey is not configured, or the endpoint, project, or Logstore is configured incorrectly. Check the parameter settings and try again. |
|
99 |
Failed to write cached data to disk. |
The write operation fails because the cache file path is incorrect, the cache is full, or the disk space is insufficient. |