-
Notifications
You must be signed in to change notification settings - Fork 0
/
logapi.go
63 lines (52 loc) · 1.55 KB
/
logapi.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
// Pakcage alilogs api interface
package alilogs
import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/galaxydi/go-loghub"
)
// AliLogAPI define log api interface
type AliLogAPI interface {
PutLogs(*sls.LogGroup) error
}
// AliLogClient implements AliLogAPI interface
type AliLogClient struct {
Endpoint string
ProjectName string
LogstoreName string
logStore *sls.LogStore
}
// PutLogs implements ali PutLogs method
func (client *AliLogClient) PutLogs(logGroup *sls.LogGroup) error {
return client.logStore.PutLogs(logGroup)
}
// NewAliLogClient ...
func NewAliLogClient(endpoint, projectName, logstoreName, accessKeyID, accessKeySecret, securityToken string) (AliLogAPI, error) {
client := AliLogClient{}
client.Endpoint = endpoint
client.ProjectName = projectName
client.LogstoreName = logstoreName
logProject, err := sls.NewLogProject(projectName, endpoint, accessKeyID, accessKeySecret)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Error("Could not get ali log project")
return nil, errors.New("Could not get ali log project")
}
if securityToken != "" {
logProject.WithToken(securityToken)
}
client.logStore, err = logProject.GetLogStore(logstoreName)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Error("Could not get ali logstore")
return nil, errors.New("Could not get ali logstore")
}
logrus.WithFields(logrus.Fields{
"endpoint": endpoint,
"projectName": projectName,
"logstoreName": logstoreName,
}).Info("Created alilogs client")
return &client, nil
}