forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathlog_config_json.go
93 lines (83 loc) · 2.28 KB
/
log_config_json.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"github.com/aliyun/aliyun-log-go-sdk"
config "github.com/aliyun/aliyun-log-go-sdk/example/config/util"
"github.com/aliyun/aliyun-log-go-sdk/example/util"
)
func main() {
// log config sample
testConf := "test-conf"
exist, err := config.CheckConfigExist(testConf)
fmt.Println(exist)
if err != nil {
fmt.Println("check conf exist fail:", err)
return
}
if exist {
config.DeleteConfig(testConf)
}
err = createJsonConfig(testConf, util.ProjectName, util.LogStoreName)
if err != nil {
fmt.Println("create config fail:", err)
return
}
fmt.Println("create json logtail config sucessed")
updateJsonConfig(testConf)
config.GetConfig(testConf)
exist, err = config.CheckConfigExist(testConf)
if err != nil {
fmt.Println(err)
return
}
if !exist {
fmt.Println("config:" + testConf + " should be exist")
return
}
config.DeleteConfig(testConf)
fmt.Println("delete json logtail config sucessed")
exist, err = config.CheckConfigExist(testConf)
if err != nil {
fmt.Println(err)
return
}
if exist {
fmt.Println("config:" + testConf + " should not be exist")
return
}
fmt.Println("log config sample end")
}
func createJsonConfig(configName string, projectName string, logstore string) (err error) {
jsonConfig := new(sls.JSONConfigInputDetail)
sls.InitJSONConfigInputDetail(jsonConfig)
// TimeKey and TimeFormat are optional, use system time as log time if not configed
jsonConfig.TimeKey = "key_time"
jsonConfig.TimeFormat = "%Y/%m/%d %H:%M:%S"
jsonConfig.LogPath = "/cloud/log/"
jsonConfig.FilePattern = "access.log*"
outputDetail := sls.OutputDetail{
ProjectName: projectName,
LogStoreName: logstore,
}
logConfig := &sls.LogConfig{
Name: configName,
InputType: "file",
OutputType: "LogService", // Now only supports LogService
InputDetail: jsonConfig,
OutputDetail: outputDetail,
}
err = util.Client.CreateConfig(projectName, logConfig)
if err != nil {
return err
}
return nil
}
func updateJsonConfig(configName string) {
logtailConfig, _ := util.Client.GetConfig(util.ProjectName, configName)
inputDetail, _ := sls.ConvertToJSONConfigInputDetail(logtailConfig.InputDetail)
inputDetail.FilePattern = "*.log"
err := util.Client.UpdateConfig(util.ProjectName, logtailConfig)
if err != nil {
panic(err)
}
}