/
tencent.go
executable file
·156 lines (136 loc) · 3 KB
/
tencent.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
package tencent
import (
"encoding/json"
"fmt"
"strconv"
"time"
cls "github.com/tencentcloud/tencentcloud-cls-sdk-go"
"google.golang.org/protobuf/proto"
log "github.com/fengleng/mars/log"
)
type Logger interface {
log.Logger
GetProducer() *cls.AsyncProducerClient
Close() error
}
type tencentLog struct {
producer *cls.AsyncProducerClient
opts *options
}
func (log *tencentLog) GetProducer() *cls.AsyncProducerClient {
return log.producer
}
type options struct {
topicID string
accessKey string
accessSecret string
endpoint string
}
func defaultOptions() *options {
return &options{}
}
func WithEndpoint(endpoint string) Option {
return func(cls *options) {
cls.endpoint = endpoint
}
}
func WithTopicID(topicID string) Option {
return func(cls *options) {
cls.topicID = topicID
}
}
func WithAccessKey(ak string) Option {
return func(cls *options) {
cls.accessKey = ak
}
}
func WithAccessSecret(as string) Option {
return func(cls *options) {
cls.accessSecret = as
}
}
type Option func(cls *options)
func (log *tencentLog) Close() error {
err := log.producer.Close(5000)
return err
}
func (log *tencentLog) Log(level log.Level, keyvals ...interface{}) error {
buf := level.String()
levelTitle := "level"
contents := make([]*cls.Log_Content, 0)
contents = append(contents, &cls.Log_Content{
Key: &levelTitle,
Value: &buf,
})
for i := 0; i < len(keyvals); i += 2 {
key := toString(keyvals[i])
value := toString(keyvals[i+1])
contents = append(contents, &cls.Log_Content{
Key: &key,
Value: &value,
})
}
logInst := &cls.Log{
Time: proto.Int64(time.Now().Unix()),
Contents: contents,
}
err := log.producer.SendLog(log.opts.topicID, logInst, nil)
return err
}
func NewLogger(options ...Option) (Logger, error) {
opts := defaultOptions()
for _, o := range options {
o(opts)
}
producerConfig := cls.GetDefaultAsyncProducerClientConfig()
producerConfig.AccessKeyID = opts.accessKey
producerConfig.AccessKeySecret = opts.accessSecret
producerConfig.Endpoint = opts.endpoint
producerInst, err := cls.NewAsyncProducerClient(producerConfig)
if err != nil {
return nil, err
}
return &tencentLog{
producer: producerInst,
opts: opts,
}, nil
}
// toString 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 fmt.Stringer:
key = v.String()
default:
newValue, _ := json.Marshal(v)
key = string(newValue)
}
return key
}