forked from aliyun/aliyun-log-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pull_log.go
69 lines (62 loc) · 1.83 KB
/
pull_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"strconv"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/example/util"
)
func main() {
sls.GlobalForceUsingHTTP = true
client := sls.CreateNormalInterface(util.Endpoint, util.AccessKeyID, util.AccessKeySecret, "")
project := util.ProjectName
logstore := util.LogStoreName
shards, err := client.ListShards(project, logstore)
if err != nil {
panic(err)
}
fmt.Printf("[shards] %v \n", shards)
now := time.Now()
totalLogCount := 0
for _, shard := range shards {
fmt.Printf("[shard] %d begin\n", shard.ShardID)
from := time.Date(now.Year(), now.Month(), 04, 19, 00, 0, 0, time.Local)
end := time.Date(now.Year(), now.Month(), 04, 20, 00, 0, 0, time.Local)
beginCursor, err := client.GetCursor(project, logstore, shard.ShardID, strconv.Itoa((int)(from.Unix())))
if err != nil {
panic(err)
}
endCursor, err := client.GetCursor(project, logstore, shard.ShardID, strconv.Itoa((int)(end.Unix())))
if err != nil {
panic(err)
}
nextCursor := beginCursor
for nextCursor != endCursor {
gl, nc, err := client.PullLogs(project, logstore, shard.ShardID, nextCursor, endCursor, 10)
if err != nil {
fmt.Printf("pull log error : %s\n", err)
time.Sleep(time.Second)
continue
}
nextCursor = nc
fmt.Printf("now count %d \n", totalLogCount)
if gl != nil {
for _, lg := range gl.LogGroups {
for _, tag := range lg.LogTags {
fmt.Printf("[tag] %s : %s\n", tag.GetKey(), tag.GetValue())
}
for _, log := range lg.Logs {
totalLogCount++
// print log
for _, content := range log.Contents {
continue
fmt.Printf("[log] %s : %s\n", content.GetKey(), content.GetValue())
}
}
}
}
}
fmt.Printf("[shard] %d done\n", shard.ShardID)
}
fmt.Printf("[total] %d \n", totalLogCount)
}