全部产品
Search
文档中心

Simple Log Service:Panduan Cepat Mulai Go SDK

更新时间:Feb 12, 2026

Topik ini menjelaskan cara menggunakan kit pengembangan perangkat lunak (SDK) Simple Log Service (SLS) untuk Go guna melakukan operasi umum, seperti membuat proyek, membuat penyimpanan log, menulis log, dan mengkueri log.

Prasyarat

Prosedur

1. Buat proyek

Name

Type

Required

Description

Example

description

string

Yes

Deskripsi proyek.

this is test

projectName

string

Yes

Nama proyek. Nama harus unik secara global dalam suatu wilayah Alibaba Cloud dan tidak dapat diubah setelah proyek dibuat. Nama harus mengikuti aturan berikut:

  • Hanya boleh berisi huruf kecil, angka, dan tanda hubung (-).

  • Harus dimulai dan diakhiri dengan huruf kecil atau angka.

  • Panjangnya harus antara 3 hingga 63 karakter.

test-project


	// Create a project.
	ProjectName := "aliyun-test-project"
	Description := "test"
	project, err := client.CreateProject(ProjectName, Description)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "ProjectAlreadyExist" {
			log.Printf("Project : %s already created or has an global name conflict in Aliyun scope", ProjectName)
		} else {
			log.Fatalf("Create project : %s failed %v", ProjectName, err)
			os.Exit(1)
		}
	} else {
		log.Printf("Project : %s created successfully", project.Name)
		time.Sleep(60 * time.Second)
	}

2. Buat Logstore

Name

Type

Required

Description

Example

project

string

Yes

Nama proyek.

ali-test-project

logstoreName

string

Yes

Nama Logstore. Nama harus mengikuti aturan berikut:

  • Nama harus unik dalam proyek.

  • Hanya boleh berisi huruf kecil, angka, tanda hubung (-), dan garis bawah (_).

  • Harus dimulai dan diakhiri dengan huruf kecil atau angka.

  • Panjangnya harus antara 3 hingga 63 karakter.

my-logstore

shardCount

int

Yes

Jumlah shard.

2

ttl

int

Yes

Periode retensi data, dalam satuan hari. Nilai yang valid: 1 hingga 3650. Nilai 3650 menunjukkan retensi permanen.

1

autoSplit

bool

No

Menentukan apakah sharding otomatis diaktifkan.

  • true: mengaktifkan sharding otomatis.

  • false: menonaktifkan sharding otomatis.

true

maxSplitShard

int

No

Jumlah maksimum shard untuk pemisahan otomatis. Nilai minimum adalah 1, dan nilai maksimum adalah 256.

Catatan

Parameter ini wajib jika autoSplit diatur ke true.

64

	// Create a Logstore.
	LogStoreName := "aliyun-test-logstore"
	var ttl, shardCnt, maxSplitShard int = 3, 2, 64
	var autoSplit bool = true
	err = client.CreateLogStore(ProjectName, LogStoreName, ttl, shardCnt, autoSplit, maxSplitShard)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "LogStoreAlreadyExist" {
			log.Printf("LogStore : %s already created", LogStoreName)
		} else {
			log.Fatalf("Create LogStore : %s failed %v", LogStoreName, err)
			os.Exit(1)
		}
	} else {
		log.Printf("Create logstore : %v successfully", LogStoreName)
		time.Sleep(10 * time.Second)
	}

3. Buat indeks

	// Create an index for the Logstore.
	index := sls.Index{
		// Field index.
		Keys: map[string]sls.IndexKey{
			"col_0": {
				Token:         []string{" "},
				CaseSensitive: false,
				Type:          "long",
			},
			"col_1": {
				Token:         []string{",", ":", " "},
				CaseSensitive: false,
				Type:          "text",
			},
		},
		// Full-text index.
		Line: &sls.IndexLine{
			Token:         []string{",", ":", " "},
			CaseSensitive: false,
			IncludeKeys:   []string{},
			ExcludeKeys:   []string{},
		},
	}
	err = client.CreateIndex(ProjectName, LogStoreName, index)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "IndexAlreadyExist" {
			log.Printf("Index : already created")
		} else {
			log.Fatalf("Create Index failed %v", err)
			os.Exit(1)
		}
	} else {
		log.Println("CreateIndex success")
		time.Sleep(60 * time.Second)
	}

4. Tulis data

Parameter

Type

Required

Description

project

string

Yes

Proyek tujuan.

logstore

string

Yes

Penyimpanan log tujuan.

topic

string

No

Topik log.

Catatan

Jika parameter ini tidak ditentukan atau dikosongkan, nilainya diatur menjadi "".

source

string

No

Kirim sumber.

Catatan

Jika parameter ini tidak ditentukan atau dikosongkan, alamat IP host tempat produsen berjalan akan digunakan.

content

Slice

Yes

Log atau daftar log yang akan dikirim. Log harus dalam format LogItem.

	// Write data to the Logstore.
	for loggroupIdx := 0; loggroupIdx < 10; loggroupIdx++ {
		logs := []*sls.Log{}
		for logIdx := 0; logIdx < 100; logIdx++ {
			content := []*sls.LogContent{}
			for colIdx := 0; colIdx < 10; colIdx++ {
				if colIdx == 0 {
					content = append(content, &sls.LogContent{
						Key:   proto.String(fmt.Sprintf("col_%d", colIdx)),
						Value: proto.String(fmt.Sprintf("%d", rand.Intn(10000000))),
					})
				} else {
					content = append(content, &sls.LogContent{
						Key:   proto.String(fmt.Sprintf("col_%d", colIdx)),
						Value: proto.String(fmt.Sprintf("loggroup idx: %d, log idx: %d, col idx: %d, value: %d", loggroupIdx, logIdx, colIdx, rand.Intn(10000000))),
					})
				}
			}
			log := &sls.Log{
				Time:     proto.Uint32(uint32(time.Now().Unix())),
				Contents: content,
			}
			logs = append(logs, log)
		}
		loggroup := &sls.LogGroup{
			Topic:  proto.String("test"),
			Source: proto.String("203.0.x.x"),
			Logs:   logs,
		}

		err = client.PutLogs(ProjectName, LogStoreName, loggroup)
		if err != nil {
			log.Fatalf("PutLogs failed %v", err)
			os.Exit(1)
		}
		log.Println("PutLogs success")
		time.Sleep(time.Second)
	}

5. Kueri data

Penting

// Query logs using SQL.
	// If a statement contains only a query statement, the line, offset, and reverse parameters are valid. The line parameter specifies the maximum number of logs to return for each query. The maximum value is 100. The offset parameter specifies the start position. You can use the line and offset parameters for paging.
	// For example, for the first query, set line to 100 and offset to 0. For the second query, set line to 100 and offset to 100.
	// If a statement contains an analytic statement, the line, offset, and reverse parameters are invalid. The result is determined by the limit, offset, and order by clauses in the analytic statement. In this case, set line to 0, offset to 0, and reverse to false. Otherwise, an error is reported.
	// For more information, see Paginate query and analysis results.
	response, err := client.GetLogs(ProjectName, LogStoreName, "test", time.Now().Unix()-1800, time.Now().Unix(), "* and col_0 > 9000000", 100, 1, true)
	if err != nil {
		log.Fatalf("GetLogs failed %v", err)
		os.Exit(1)
	}
	log.Printf("Get %d logs", response.Count)
	logs := response.Logs
	for i := range logs {
		for k, v := range logs[i] {
			log.Printf("key: %s, value: %s", k, v)
		}
		log.Println("======")
	}

Kode contoh

Contoh ini membuat file SLSQuickStart.go dan memanggil operasi API untuk membuat proyek, membuat penyimpanan log, membuat indeks, menulis data, serta mengkueri data. Kode berikut menunjukkan contohnya:

package main

import (
	"fmt"
	"log"
	"math/rand"
	"os"
	"time"

	sls "github.com/aliyun/aliyun-log-go-sdk"
	"github.com/gogo/protobuf/proto"
)

func main() {
	// The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
	Endpoint := "cn-hangzhou.log.aliyuncs.com"

	// This example obtains the AccessKey ID and AccessKey secret from environment variables.
	AccessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
	AccessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
	// The temporary security token for the RAM user role. An empty value indicates that no temporary security token is used.
	SecurityToken := ""
	// Create a Simple Log Service client.
	provider := sls.NewStaticCredentialsProvider(AccessKeyId, AccessKeySecret, SecurityToken)
	client := sls.CreateNormalInterfaceV2(Endpoint, provider)

	// Create a project.
	ProjectName := "aliyun-test-project"
	Description := "test"
	project, err := client.CreateProject(ProjectName, Description)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "ProjectAlreadyExist" {
			log.Printf("Project : %s already created or has an global name conflict in Aliyun scope", ProjectName)
		} else {
			log.Fatalf("Create project : %s failed %v", ProjectName, err)
			os.Exit(1)
		}
	} else {
		log.Printf("Project : %s created successfully", project.Name)
		time.Sleep(60 * time.Second)
	}

	// Create a Logstore.
	LogStoreName := "aliyun-test-logstore"
	err = client.CreateLogStore(ProjectName, LogStoreName, 3, 2, true, 6)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "LogStoreAlreadyExist" {
			log.Printf("LogStore : %s already created", LogStoreName)
		} else {
			log.Fatalf("Create LogStore : %s failed %v", LogStoreName, err)
			os.Exit(1)
		}
	} else {
		log.Printf("Create logstore : %v successfully", LogStoreName)
		time.Sleep(10 * time.Second)
	}

	// Create an index for the Logstore.
	index := sls.Index{
		// Field index.
		Keys: map[string]sls.IndexKey{
			"col_0": {
				Token:         []string{" "},
				CaseSensitive: false,
				Type:          "long",
			},
			"col_1": {
				Token:         []string{",", ":", " "},
				CaseSensitive: false,
				Type:          "text",
			},
		},
		// Full-text index.
		Line: &sls.IndexLine{
			Token:         []string{",", ":", " "},
			CaseSensitive: false,
			IncludeKeys:   []string{},
			ExcludeKeys:   []string{},
		},
	}
	err = client.CreateIndex(ProjectName, LogStoreName, index)
	if err != nil {
		if e, ok := err.(*sls.Error); ok && e.Code == "IndexAlreadyExist" {
			log.Printf("Index : already created")
		} else {
			log.Fatalf("Create Index failed %v", err)
			os.Exit(1)
		}
	} else {
		log.Println("CreateIndex success")
		time.Sleep(60 * time.Second)
	}

	// Write data to the Logstore.
	for loggroupIdx := 0; loggroupIdx < 10; loggroupIdx++ {
		logs := []*sls.Log{}
		for logIdx := 0; logIdx < 100; logIdx++ {
			content := []*sls.LogContent{}
			for colIdx := 0; colIdx < 10; colIdx++ {
				if colIdx == 0 {
					content = append(content, &sls.LogContent{
						Key:   proto.String(fmt.Sprintf("col_%d", colIdx)),
						Value: proto.String(fmt.Sprintf("%d", rand.Intn(10000000))),
					})
				} else {
					content = append(content, &sls.LogContent{
						Key:   proto.String(fmt.Sprintf("col_%d", colIdx)),
						Value: proto.String(fmt.Sprintf("loggroup idx: %d, log idx: %d, col idx: %d, value: %d", loggroupIdx, logIdx, colIdx, rand.Intn(10000000))),
					})
				}
			}
			log := &sls.Log{
				Time:     proto.Uint32(uint32(time.Now().Unix())),
				Contents: content,
			}
			logs = append(logs, log)
		}
		loggroup := &sls.LogGroup{
			Topic:  proto.String("test"),
			Source: proto.String("203.0.113.10"),
			Logs:   logs,
		}

		err = client.PutLogs(ProjectName, LogStoreName, loggroup)
		if err != nil {
			log.Fatalf("PutLogs failed %v", err)
			os.Exit(1)
		}
		log.Println("PutLogs success")
		time.Sleep(time.Second)
	}

	// Query logs using SQL.
	// If a statement contains only a query statement, the line, offset, and reverse parameters are valid. The line parameter specifies the maximum number of logs to return for each query. The maximum value is 100. The offset parameter specifies the start position. You can use the line and offset parameters for paging.
	// For example, for the first query, set line to 100 and offset to 0. For the second query, set line to 100 and offset to 100.
	// If a statement contains an analytic statement, the line, offset, and reverse parameters are invalid. The result is determined by the limit, offset, and order by clauses in the analytic statement. In this case, set line to 0, offset to 0, and reverse to false. Otherwise, an error is reported.
	// For more information, see Paginate query and analysis results.
	response, err := client.GetLogs(ProjectName, LogStoreName, "test", time.Now().Unix()-1800, time.Now().Unix(), "* and col_0 > 9000000", 100, 1, true)
	if err != nil {
		log.Fatalf("GetLogs failed %v", err)
		os.Exit(1)
	}
	log.Printf("Get %d logs", response.Count)
	logs := response.Logs
	for i := range logs {
		for k, v := range logs[i] {
			log.Printf("key: %s, value: %s", k, v)
		}
		log.Println("======")
	}
}

Untuk kode contoh lainnya, lihat Aliyun Log Go SDK.

Hasil

Kode berikut menunjukkan contoh hasilnya:

Project : aliyun-test-project created successfully.
Create logstore : aliyun-test-logstore successfully.
CreateIndex success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
PutLogs success
Get 61 logs
key: source, value: 203.0.113.10
key: time, value: 1627282116
key: col_0, value: 9886757
key: col_1, value: loggroup idx: 6, log idx: 87, col idx: 1, value: 2673724
key: col_2, value: loggroup idx: 6, log idx: 87, col idx: 2, value: 5822012
key: col_8, value: loggroup idx: 6, log idx: 87, col idx: 8, value: 3996746
key: topic, value: test
key: col_9, value: loggroup idx: 6, log idx: 87, col idx: 9, value: 7646111
key: col_3, value: loggroup idx: 6, log idx: 87, col idx: 3, value: 8872632
key: col_4, value: loggroup idx: 6, log idx: 87, col idx: 4, value: 1839836
key: col_5, value: loggroup idx: 6, log idx: 87, col idx: 5, value: 6967415
key: col_6, value: loggroup idx: 6, log idx: 87, col idx: 6, value: 5872057
key: col_7, value: loggroup idx: 6, log idx: 87, col idx: 7, value: 3227909
======
......