tencent cloud

Mini Program JavaScript SDK Log Upload
Last updated:2025-11-07 17:40:38
Mini Program JavaScript SDK Log Upload
Last updated: 2025-11-07 17:40:38
This article introduces how to quickly get started with the Mini Program JavaScript SDK of Cloud Log Service (CLS) to implement log upload. For more details on SDK usage, visit the code repository tencentcloud-cls-sdk-js-mini.

Prerequisites

Please ensure the log topic has anonymous log collection enabled.

Preparing a Development Environment

Before starting access, please first download and install Node.js. For the download link and related operations, view the Node.js official website.
For CentOS systems, run the following command to install the development environment.
# Install EPEL repository
sudo yum install epel-release -y
# Install Node.js and npm
sudo yum install nodejs npm -y

Installing the Mini Program JavaScript SDK

1. Create and enter the project directory.
2. Install the SDK via npm.
npm install
npm install tencentcloud-cls-sdk-js-mini

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information. Fill in the domain name in the available region under the Log upload via API Tab.
retry_times
integer
Yes
Maximum number of retries
Source
String
No
Source IP
topicID
String
Yes
Log topic ID information.

Log Upload Example Code

The following code uses the Mini Program JavaScript SDK to show how to complete log upload by calling the SDK.
const { Log, LogGroup, AsyncClient, PutLogsRequest } = require('tencentcloud-cls-sdk-js-mini');

// Create an AsyncClient instance
let client = new AsyncClient({
endpoint: "ap-xxxxxx.cls.tencentcs.com", // Fill in domain information, see the domain name in the Log upload via API Tab: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8D
retry_times: 10,
});

// Create a LogGroup instance
let logGroup = new LogGroup();
logGroup.setSource("1.XX.XX.XX"); // Replace with your IP address

// Create a Log instance
let log = new Log(Date.now());
log.addContent("hello", "hello world1");
log.addContent("world", "hello world2");

// Add Log to LogGroup
logGroup.addLog(log);

// Create a PutLogsRequest instance
let request = new PutLogsRequest("8bbfXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", logGroup); // replace with your log topic ID

// Upload logs
async function uploadLogs() {
try {
let data = await client.PutLogs(request);
console.log("Logs uploaded successfully:", data);
} catch (error) {
console.error("Error uploading logs:", error);
}
}

// Call the log upload function
uploadLogs();

Conclusions

Through these steps, you can quickly use the Mini Program JavaScript SDK of Tencent Cloud CLS to complete log upload. 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