本文介绍如何快速使用日志服务Go SDK完成常见操作,包括创建项目(Project)、创建日志库(Logstore)、写入日志和查询日志等。
前提条件
操作步骤
1.创建Project
2.创建Logstore
3.创建索引
4.写入数据
示例代码
本示例中,创建一个SLSQuickStart.go文件,并调用接口分别完成创建Project、创建Logstore、创建索引、写入日志数据和查询日志数据。示例如下:
package main
import (
"fmt"
"log"
"math/rand"
"os"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/gogo/protobuf/proto"
)
func main() {
// 日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
Endpoint := "cn-hangzhou.log.aliyuncs.com"
// 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
AccessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
AccessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// RAM用户角色的临时安全令牌。此处取值为空,表示不使用临时安全令牌。
SecurityToken := ""
// 创建日志服务Client。
provider := sls.NewStaticCredentialsProvider(AccessKeyId, AccessKeySecret, SecurityToken)
client := sls.CreateNormalInterfaceV2(Endpoint, provider)
// 创建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)
}
// 创建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)
}
// 为Logstore创建索引。
index := sls.Index{
// 字段索引。
Keys: map[string]sls.IndexKey{
"col_0": {
Token: []string{" "},
CaseSensitive: false,
Type: "long",
},
"col_1": {
Token: []string{",", ":", " "},
CaseSensitive: false,
Type: "text",
},
},
// 全文索引。
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)
}
// 向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)
}
// 通过SQL查询日志。
// 当语句仅含查询语句时,line、offset、reverse取值有效。line为每次获取的最大日志条数,最大值为100,offset为起始位置。通过line与offset参数可以实现翻页效果。
// 例如首次查询line=100、offset=0, 第二次查询line = 100、offset=100。
// 当语句包含分析语句时,line、offset、reverse取值无效,以分析语句的limit、offset、order by子句为准,此时建议line、offset、reverse填写0、0、false(其他值会报错)。
// 更多信息,请参见分页显示查询分析结果。
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("======")
}
}
更多示例代码,请参见Aliyun Log Go SDK。
返回结果
返回结果示例如下:
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
======
......