All Products
Search
Document Center

Simple Log Service:Get started with Simple Log Service SDK for Go

Last Updated:Dec 11, 2023

This topic describes how to get started with Simple Log Service SDK for Go and perform common operations. For example, you can create a project, create a Logstore, write logs, and query logs.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Go is installed. For more information, see Install Simple Log Service SDK for Go.

  • The proto dependency is installed. You can run the go get -u github.com/gogo/protobuf/proto command to install the proto dependency.

Sample code

In this example, a file named SLSQuickStart.go is created. The sample code in this file provides an example on how to call API operations to create a project, create a Logstore, create indexes, write logs, and query logs. Example:

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 Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    Endpoint := "cn-hangzhou.log.aliyuncs.com"

    // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    AccessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    AccessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") 
    // The Security Token Service (STS) token of the RAM user. If you leave this parameter empty, no STS tokens are 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 indexes for the Logstore. 
    index := sls.Index{
        // Field indexing. 
        Keys: map[string]sls.IndexKey{
            "col_0": {
                Token:         []string{" "},
                CaseSensitive: false,
                Type:          "long",
            },
            "col_1": {
                Token:         []string{",", ":", " "},
                CaseSensitive: false,
                Type:          "text",
            },
        },
        // Full-text indexing. 
        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 logs 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)
    }

    // Execute an SQL statement to query logs. 
    // If the query statement includes only a search statement, the values of the line, offset, and reverse parameters take effect. The line parameter specifies the maximum number of logs that can be returned. Maximum value: 100. The offset parameter specifies the start position of the returned logs. To paginate logs, you can configure the line and offset parameters. 
    // For example, you can set the line parameter to 100 and the offset parameter to 0 for the first query, and set the line parameter to 100 and the offset parameter to 100 for the second query. 
    // If the query statement includes an analytic statement, the values of the line, offset, and reverse parameters do not take effect. The number of logs that are returned is determined by the LIMIT, OFFSET, and ORDER BY clauses. We recommend that you set the value of the line, offset, and reverse parameters to 0, 0, and false. If you specify other values for the parameters, errors are reported. 
    For more information, see Paged query. 
    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("======")
    }
}

For more information about sample code, see Alibaba Cloud Simple Log Service SDK for Go.

Response

The following response is returned for the preceding example:

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
======
......