-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlyslsAppender.go
130 lines (101 loc) · 2.82 KB
/
AlyslsAppender.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
123
124
125
126
127
128
129
130
package logx
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/gogo/protobuf/proto"
"github.com/lamgor666/goboot-common/enum/DatetimeFormat"
"regexp"
"strings"
"time"
)
type alyslsAppender struct {
}
func (a *alyslsAppender) GetAppenderName() string {
return "SlyslsAppender"
}
func (a *alyslsAppender) Write(buf []byte) (int, error) {
st := globalAlyslsSettings
if st.appid == "" || st.appsecret == "" || st.projectName == "" || st.logstoreName == "" {
return len(buf), nil
}
parts := strings.Split(string(buf), fieldSep)
var ts, channel, level, msg string
for _, p := range parts {
if strings.HasPrefix(p, "ts:") {
ts = strings.TrimPrefix(p, "ts:")
continue
}
if strings.HasPrefix(p, "channel:") {
channel = strings.TrimPrefix(p, "channel:")
continue
}
if strings.HasPrefix(p, "level:") {
level = strings.TrimPrefix(p, "level:")
continue
}
if strings.HasPrefix(p, "msg:") {
msg = strings.TrimPrefix(p, "msg:")
}
}
msgTags, msg := a.handleMsgTags(msg)
contents := []*sls.LogContent{
{Key: proto.String("ts"), Value: proto.String(ts)},
{Key: proto.String("channel"), Value: proto.String(channel)},
{Key: proto.String("level"), Value: proto.String(level)},
{Key: proto.String("message"), Value: proto.String(msg)},
}
loc, _ := time.LoadLocation("Asia/Shanghai")
t1, _ := time.ParseInLocation(DatetimeFormat.Full, ts, loc)
logItem := &sls.Log{
Time: proto.Uint32(uint32(t1.Unix())),
Contents: contents,
}
logs := []*sls.Log{logItem}
projectName := st.projectName
storeName := st.logstoreName
tags := []*sls.LogTag{
{Key: proto.String("project"), Value: proto.String(projectName)},
{Key: proto.String("store"), Value: proto.String(storeName)},
}
for _, parts := range msgTags {
tags = append(tags, &sls.LogTag{
Key: proto.String(parts[0]),
Value: proto.String(parts[1]),
})
}
group := &sls.LogGroup{Logs: logs, LogTags: tags}
client := sls.CreateNormalInterface(
st.apiDomain,
st.appid,
st.appsecret,
"",
)
client.PutLogs(projectName, storeName, group)
client.Close()
return len(buf), nil
}
func (a *alyslsAppender) handleMsgTags(msg string) ([][]string, string) {
re1 := regexp.MustCompile(`^tags[\x20\t]*=[\x20\t]*\[([^]]+)]`)
tags := make([][]string, 0)
groups := re1.FindStringSubmatch(msg)
if len(groups) < 2 {
return tags, strings.TrimSpace(msg)
}
msg = strings.TrimPrefix(msg, groups[0])
msg = strings.TrimSpace(msg)
re2 := regexp.MustCompile(`[\x20\t]+`)
parts := re2.Split(strings.TrimSpace(groups[1]), -1)
re3 := regexp.MustCompile(`[\x20\t]*:[\x20\t]*`)
for _, p := range parts {
a1 := re3.Split(p, -1)
if len(a1) != 2 {
continue
}
p1 := strings.TrimSpace(a1[0])
p2 := strings.TrimSpace(a1[1])
if p1 == "" || p2 == "" {
continue
}
tags = append(tags, []string{p1, p2})
}
return tags, msg
}