-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathlog_config_sample.go
122 lines (110 loc) · 2.67 KB
/
log_config_sample.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"os"
sls "github.com/galaxydi/go-loghub"
"github.com/galaxydi/go-loghub/example/util"
)
var projectName = "another-project"
var logstore = "demo-store"
func main() {
// log config sample
testConf := "test-conf"
testService := "demo-service"
exist, err := checkConfigExist(testConf)
if err != nil {
fmt.Println("check conf exist fail:", err)
os.Exit(1)
}
if exist {
deleteConfig(testConf)
}
err = createConfig(testConf, projectName, logstore, testService)
if err != nil {
fmt.Println("create config fail:", err)
os.Exit(1)
}
fmt.Println("create config success")
updateConfig(testConf)
getConfig(testConf)
exist, err = checkConfigExist(testConf)
if err != nil {
os.Exit(1)
}
if !exist {
fmt.Println("config:" + testConf + " should be exist")
os.Exit(1)
}
deleteConfig(testConf)
exist, err = checkConfigExist(testConf)
if err != nil {
os.Exit(1)
}
if exist {
fmt.Println("config:" + testConf + " should not be exist")
os.Exit(1)
}
fmt.Println("log config sample end")
}
func checkConfigExist(confName string) (exist bool, err error) {
exist, err = util.Project.CheckConfigExist(confName)
if err != nil {
return false, err
}
return exist, nil
}
func deleteConfig(confName string) (err error) {
err = util.Project.DeleteConfig(confName)
if err != nil {
return err
}
return nil
}
func updateConfig(configName string) (err error) {
config, _ := util.Project.GetConfig(configName)
config.InputDetail.FilePattern = "*.log"
err = util.Project.UpdateConfig(config)
if err != nil {
return err
}
return nil
}
func getConfig(configName string) (err error) {
_, err = util.Project.GetConfig(configName)
if err != nil {
return err
}
return nil
}
func createConfig(configName string, projectName string, logstore string, serviceName string) (err error) {
keys := []string{"message"}
inputDetail := sls.InputDetail{
LogType: "common_reg_log",
LogPath: "/var/log/lambda/" + serviceName,
FilePattern: "*.LOG",
TopicFormat: "/var/log/lambda/([^/]*)/.*",
LocalStorage: true,
TimeFormat: "",
LogBeginRegex: ".*", // 日志首行特征
Regex: "(.*)", // 日志对提取正则表达式
Keys: keys,
FilterKeys: make([]string, 1),
FilterRegex: make([]string, 1),
}
outputDetail := sls.OutputDetail{
ProjectName: projectName,
LogStoreName: logstore,
}
config := &sls.LogConfig{
Name: configName,
InputType: "file", //现在只支持file
OutputType: "LogService", //现在只支持LogService
InputDetail: inputDetail,
OutputDetail: outputDetail,
}
err = util.Project.CreateConfig(config)
if err != nil {
return err
}
return nil
}