tencent cloud

Cloud Log Service

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Features
Available Regions
Limits
Concepts
Service Regions and Service Providers
Purchase Guide
Billing Overview
Product Pricing
Pay-as-You-Go
Billing
Cleaning up CLS resources
Cost Optimization
FAQs
Getting Started
Getting Started in 1 Minute
Getting Started Guide
Quickly Trying out CLS with Demo
Operation Guide
Resource Management
Permission Management
Log Collection
Metric Collection
Log Storage
Metric Storage
Search and Analysis (Log Topic)
Search and Analysis (Metric Topic)
Dashboard
Data Processing documents
Shipping and Consumption
Monitoring Alarm
Cloud Insight
Independent DataSight console
Historical Documentation
Practical Tutorial
Log Collection
Search and Analysis
Dashboard
Monitoring Alarm
Shipping and Consumption
Cost Optimization
Developer Guide
Embedding CLS Console
CLS Connection to Grafana
API Documentation
History
Introduction
API Category
Making API Requests
Topic Management APIs
Log Set Management APIs
Index APIs
Topic Partition APIs
Machine Group APIs
Collection Configuration APIs
Log APIs
Metric APIs
Alarm Policy APIs
Data Processing APIs
Kafka Protocol Consumption APIs
CKafka Shipping Task APIs
Kafka Data Subscription APIs
COS Shipping Task APIs
SCF Delivery Task APIs
Scheduled SQL Analysis APIs
COS Data Import Task APIs
Data Types
Error Codes
FAQs
Health Check
Collection
Log Search
Others
CLS Service Level Agreement
CLS Policy
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary

GO SDK Log Upload

PDF
Focus Mode
Font Size
Last updated: 2025-11-07 17:40:37
This article introduces how to quickly get started with the Go SDK of Cloud Log Service (CLS) to implement log upload operations. For more details on SDK usage, visit the code repository tencentcloud-cls-sdk-go.

Prerequisites

Create and obtain TencentCloud API key information (AccessKeyID and AccessKeySecret). 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 Go official website to download and install the Go development environment.
After installing Go, create system variable GOPATH and point it to your code directory.

Installing Go SDK

Run the following command in the command line tool to install the Go SDK.
go get github.com/tencentcloud/tencentcloud-cls-sdk-go

Introducing CLS Go SDK

When writing the code script, reference the CLS Go SDK in your Go code.
import (
"fmt"
"github.com/TencentCloud/tencentcloud-cls-sdk-go"
)

Request Parameters

Variable
Type
Required
Description
Endpoint
String
Yes
Domain information. For completion, see the domain name in the Log upload via API Tab of available region.
AccessKeyID
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.
AccessKeySecret
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 information.

Log Upload Example Code

The following code uses the Go SDK as an example to show how to complete log upload operations by calling the SDK. Sample code:
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain API key information through environment variables. For detailed operations, please see configure environment variables.
package main
// Import the CLS Go SDK
import (
"fmt"
"github.com/tencentcloud/tencentcloud-cls-sdk-go"
"sync"
"os"
"time"
)

func main() {
producerConfig := tencentcloud_cls_sdk_go.GetDefaultAsyncProducerClientConfig()
// 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 the domain name in the Log upload via API Tab of the link.
producerConfig.Endpoint = "ap-XXXXXXXXX.cls.xxxxxxx.com"
// Fill in TencentCloud 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. Permission configuration guide: 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. Environment variable configuration guide: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
producerConfig.AccessKeyID = os.Getenv("TENCENTCLOUD_SECRET_ID")
producerConfig.AccessKeySecret = os.Getenv("TENCENTCLOUD_SECRET_KEY")
// Set the topic ID for log upload, replace with your Topic ID
topicId := "8bbXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
// Create an asynchronous producer client instance
producerInstance, err := tencentcloud_cls_sdk_go.NewAsyncProducerClient(producerConfig)
if err != nil {
fmt.Println(err)
return
}

// Start the asynchronous sender program
producerInstance.Start()

var m sync.WaitGroup
callBack := &Callback{}
for i := 0; i < 10; i++ {
m.Add(1)
go func() {
defer m.Done()
for i := 0; i < 1000; i++ {
// Create a new log with the current timestamp and log content
log := tencentcloud_cls_sdk_go.NewCLSLog(time.Now().Unix(), map[string]string{"content": "hello world| I'm from XXX", "content2": fmt.Sprintf("%v", i)})
err = producerInstance.SendLog(topicId, log, callBack)
if err != nil {
fmt.Println(err)
continue
}
}
}()
}
m.Wait()
producerInstance.Close(60000)
}

type Callback struct {
}

func (callback *Callback) Success(result *tencentcloud_cls_sdk_go.Result) {
attemptList := result.GetReservedAttempts()
for _, attempt := range attemptList {
fmt.Printf("%+v \\n", attempt)
}
}

func (callback *Callback) Fail(result *tencentcloud_cls_sdk_go.Result) {
fmt.Println(result.IsSuccessful())
fmt.Println(result.GetErrorCode())
fmt.Println(result.GetErrorMessage())
fmt.Println(result.GetReservedAttempts())
fmt.Println(result.GetRequestId())
fmt.Println(result.GetTimeStampMs())
}

Conclusions

Through these steps, you can quickly use the Tencent Cloud CLS Go SDK to complete log upload. If you encounter any issues, please contact us to get help.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback