/
esx.go
62 lines (53 loc) · 1.43 KB
/
esx.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
package elastic5x
import (
"github.com/chen56/go-common/must"
"github.com/sirupsen/logrus"
"gopkg.in/olivere/elastic.v5"
)
//Conf https://github.com/olivere/elastic/wiki/Sniffing
type Conf struct {
URL string `yaml:"url" json:"url"`
Sniff bool `yaml:"sniff" json:"sniff"`
Healthcheck bool `yaml:"healthcheck" json:"healthcheck"`
Errorlog elastic.Logger // error log for critical messages
Infolog elastic.Logger // information log for e.g. response times
Tracelog elastic.Logger // trace log for debugging
}
//Client elastic.Client 扩展
type Client struct {
*elastic.Client
}
//MustNewClient 新建Client
func (c Conf) MustNewClient() *Client {
var errorlog = c.Errorlog
if errorlog == nil {
errorlog = &esLogAdapter{logrus.ErrorLevel}
}
var infolog = c.Infolog
if infolog == nil {
infolog = &esLogAdapter{logrus.InfoLevel}
}
var tracelog = c.Tracelog
if tracelog == nil {
tracelog = &esLogAdapter{logrus.TraceLevel}
}
client, err := elastic.NewClient(
elastic.SetSniff(c.Sniff),
elastic.SetURL(c.URL),
elastic.SetHealthcheck(c.Healthcheck),
//定制日志转发
elastic.SetErrorLog(errorlog),
elastic.SetInfoLog(infolog),
elastic.SetTraceLog(tracelog),
)
must.NoError(err)
return &Client{
client,
}
}
type esLogAdapter struct {
level logrus.Level
}
func (l *esLogAdapter) Printf(format string, v ...interface{}) {
logrus.StandardLogger().Logf(l.level, format, v...)
}