代码拉取完成,页面将自动刷新
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()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。