-
Notifications
You must be signed in to change notification settings - Fork 1
/
aliyun.go
executable file
·172 lines (147 loc) · 3.3 KB
/
aliyun.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package aliyun
import (
"encoding/json"
"strconv"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/producer"
"google.golang.org/protobuf/proto"
log "github.com/fengleng/mars/log"
)
// Logger see more detail https://github.com/aliyun/aliyun-log-go-sdk
type Logger interface {
log.Logger
GetProducer() *producer.Producer
Close() error
}
type aliyunLog struct {
producer *producer.Producer
opts *options
}
func (a *aliyunLog) GetProducer() *producer.Producer {
return a.producer
}
type options struct {
accessKey string
accessSecret string
endpoint string
project string
logstore string
}
func defaultOptions() *options {
return &options{
project: "projectName",
logstore: "app",
}
}
func WithEndpoint(endpoint string) Option {
return func(alc *options) {
alc.endpoint = endpoint
}
}
func WithProject(project string) Option {
return func(alc *options) {
alc.project = project
}
}
func WithLogstore(logstore string) Option {
return func(alc *options) {
alc.logstore = logstore
}
}
func WithAccessKey(ak string) Option {
return func(alc *options) {
alc.accessKey = ak
}
}
func WithAccessSecret(as string) Option {
return func(alc *options) {
alc.accessSecret = as
}
}
type Option func(alc *options)
func (a *aliyunLog) Close() error {
return a.producer.Close(5000)
}
func (a *aliyunLog) Log(level log.Level, keyvals ...interface{}) error {
buf := level.String()
levelTitle := "level"
contents := make([]*sls.LogContent, 0)
contents = append(contents, &sls.LogContent{
Key: &levelTitle,
Value: &buf,
})
for i := 0; i < len(keyvals); i += 2 {
key := toString(keyvals[i])
value := toString(keyvals[i+1])
contents = append(contents, &sls.LogContent{
Key: &key,
Value: &value,
})
}
logInst := &sls.Log{
Time: proto.Uint32(uint32(time.Now().Unix())),
Contents: contents,
}
err := a.producer.SendLog(a.opts.project, a.opts.logstore, "", "", logInst)
return err
}
// NewAliyunLog new a aliyun logger with options.
func NewAliyunLog(options ...Option) Logger {
opts := defaultOptions()
for _, o := range options {
o(opts)
}
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = opts.endpoint
producerConfig.AccessKeyID = opts.accessKey
producerConfig.AccessKeySecret = opts.accessSecret
producerInst := producer.InitProducer(producerConfig)
return &aliyunLog{
opts: opts,
producer: producerInst,
}
}
// toString convert any type to string
func toString(v interface{}) string {
var key string
if v == nil {
return key
}
switch v := v.(type) {
case float64:
key = strconv.FormatFloat(v, 'f', -1, 64)
case float32:
key = strconv.FormatFloat(float64(v), 'f', -1, 64)
case int:
key = strconv.Itoa(v)
case uint:
key = strconv.Itoa(int(v))
case int8:
key = strconv.Itoa(int(v))
case uint8:
key = strconv.Itoa(int(v))
case int16:
key = strconv.Itoa(int(v))
case uint16:
key = strconv.Itoa(int(v))
case int32:
key = strconv.Itoa(int(v))
case uint32:
key = strconv.Itoa(int(v))
case int64:
key = strconv.FormatInt(v, 10)
case uint64:
key = strconv.FormatUint(v, 10)
case string:
key = v
case bool:
key = strconv.FormatBool(v)
case []byte:
key = string(v)
default:
newValue, _ := json.Marshal(v)
key = string(newValue)
}
return key
}