tencent cloud

Browser JavaScript SDK Log Upload
Last updated:2025-11-07 17:40:37
Browser JavaScript SDK Log Upload
Last updated: 2025-11-07 17:40:37
This article introduces how to quickly get started with the browser 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-web.

Prerequisites

Please ensure the log topic enables anonymous log collection.

Preparing the Development Environment

Before starting access, please first download and install Node.js. Check the Node.js official website for the download link and related operations.
For CentOS system, use 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 Browser JavaScript SDK

1. Create and enter the project directory.
git clone https://github.com/TencentCloud/tencentcloud-cls-sdk-js.git
cd tencentcloud-cls-sdk-js
2. Install the SDK via npm.
npm install
npm install tencentcloud-cls-sdk-js-web

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information. For details, fill in the domain name in the available region under the API Log Upload Tab.
retry_times
integer
Yes
Maximum number of retries
Source
String
No
Source IP address.
topicID
String
Yes
Log topic ID info.

Log Upload Code Example

The following code shows how to complete log upload operation by calling the SDK in browser JavaScript.
const { Log, LogGroup, AsyncClient, PutLogsRequest } = require('tencentcloud-cls-sdk-js-web');

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

// Create a LogGroup instance
let logGroup = new LogGroup();
logGroup.setSource("1.X.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("8bbXXXXXXXXXXXXXXXXXXXXXXXXXXXX", logGroup); // Replace with your topicID

// 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 browser 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