Ejemplo n.º 1
0
// NewElasticStore 创建基于ElasticSearch存储的实例
func NewElasticStore(cfg log.ElasticConfig) log.LogStore {
	if cfg.URL == "" {
		cfg.URL = log.DefaultElasticURL
	}
	if cfg.IndexTmpl == "" {
		cfg.IndexTmpl = log.DefaultElasticIndexTmpl
	}
	if cfg.TypeTmpl == "" {
		cfg.TypeTmpl = log.DefaultElasticTypeTmpl
	}
	client, err := elastic.NewClient(elastic.SetURL(cfg.URL), elastic.SetSniff(false))
	if err != nil {
		panic(err)
	}
	return &ElasticStore{
		client:    client,
		indexTmpl: template.Must(template.New("").Parse(cfg.IndexTmpl)),
		typeTmpl:  template.Must(template.New("").Parse(cfg.TypeTmpl)),
	}
}