1 Star 0 Fork 0

magicianlyx/GoLog

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log_es.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
package GoLog
import (
"context"
"github.com/olivere/elastic/v7"
)
// +---------------------------------------------------------+
// + +
// + EsLog +
// + +
// +---------------------------------------------------------+
// 使用es
type EsLog struct {
log *BufferLog
client *elastic.Client
}
func newEsClient(url string) (*elastic.Client, error) {
options := []elastic.ClientOptionFunc{
elastic.SetSniff(false),
elastic.SetURL(url),
}
client, err := elastic.NewClient(
options...,
)
return client, err
}
func NewEsLog(url string, index string) (*EsLog, error) {
client, err := newEsClient(url)
if err != nil {
return nil, err
}
bulkRequest := client.Bulk()
ctx := context.Background()
writer := func(msg IMsg) {
r := msg.ToMap()
indexReq := elastic.NewBulkIndexRequest().Index(index)
indexReq = indexReq.Doc(r)
bulkRequest = bulkRequest.Add(indexReq)
_, _ = bulkRequest.Do(ctx)
}
log := NewBufferLog(50, writer)
return &EsLog{log: log, client: client}, nil
}
func (c *EsLog) Write(msg IMsg) {
c.log.Write(msg)
}
func (c *EsLog) Stop() {
c.log.Stop()
c.client.Stop()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoLog.git
git@gitee.com:magicianlyx/GoLog.git
magicianlyx
GoLog
GoLog
20c45f9b998d

搜索帮助