tencent cloud

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

Prerequisites

Create and obtain TencentCloud API key information (secretId and secretKey). For key information acquisition, please visit API Key Management.
Please ensure the associated account has appropriate SDK log upload permission.

Preparing the Development Environment

See Java official website to download and install Java development environment.
CLS Java SDK supports Java runtime environment JRE 6.0 or higher. Run the earlier command to check the current Java Version.
java -version

Installing Java SDK

It is recommended to introduce the Java SDK by adding the dependency in a Maven project.
To use the CLS Java SDK in a Maven project, just add the dependency in pom.xml. The project management tool will automatically download the related JAR packages.
<dependency>
<groupId>com.tencentcloudapi.cls</groupId>
<artifactId>tencentcloud-cls-sdk-java</artifactId>
<version>1.0.15</version>
</dependency>

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information, please refer to the domain name in the available region under the Log upload via API Tab.
secretId
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.
secretKey
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.
topicId
String
Yes
Log topic ID info.

Upload Example Code for Logs

Using Java SDK as an example, it shows the operation of log upload by calling the SDK. The sample code is as follows.
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain the API key information through environment variables. For detailed operations, please refer to configure environment variables.
import com.tencentcloudapi.cls.sdk.AsyncProducerClient;
import com.tencentcloudapi.cls.sdk.AsyncProducerConfig;
import com.tencentcloudapi.cls.sdk.LogItem;
import com.tencentcloudapi.cls.sdk.LogContent;
import com.tencentcloudapi.cls.sdk.NetworkUtils;

import java.util.ArrayList;
import java.util.List;

public class TencentClsDemo {
public static void main(String[] args) {
// Fill in the 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
String endpoint = "ap-xxxxxx.cls.tencentcs.com";
// Fill in the cloud API Key Information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi
// Please ensure the associated account has appropriate log upload permissions. For the permission configuration guide, see: https://www.tencentcloud.com/document/product/614/68374?from_cn_redirect=1#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE
// This example retrieves from environmental variable. For environment variable configuration guide, see: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
String secretId = System.getenv("TENCENTCLOUD_SECRET_ID");
String secretKey = System.getenv("TENCENTCLOUD_SECRET_KEY");
// fill in the log topic ID
String topicId = "5912XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

final AsyncProducerConfig config = new AsyncProducerConfig(endpoint, secretId, secretKey, NetworkUtils.getLocalMachineIP());

// Build a client instance.
final AsyncProducerClient client = new AsyncProducerClient(config);

for (int i = 0; i < 10000; ++i) {
List<LogItem> logItems = new ArrayList<>();
int ts = (int) (System.currentTimeMillis() / 1000);
LogItem logItem = new LogItem(ts);
logItem.PushBack(new LogContent("__CONTENT__", "hello world"));
logItem.PushBack(new LogContent("city", "guangXXXXX"));
logItem.PushBack(new LogContent("logNo", Integer.toString(i)));
logItem.PushBack(new LogContent("__PKG_LOGID__", (String.valueOf(System.currentTimeMillis()))));
logItems.add(logItem);
client.putLogs(topicId, logItems, result -> System.out.println(result.toString()));
}
client.close();
}
}

Conclusions

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

Feedback