Beispiel #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)),
	}
}
Beispiel #2
0
func TestElasticStore(t *testing.T) {
	var cfg log.ElasticConfig
	cfg.URL = "http://192.168.33.70:9200"
	store := NewElasticStore(cfg)
	var err error
	for i := 0; i < 1000; i++ {
		var item log.LogItem
		item.ID = uint64(i)
		item.Time = time.Now()
		item.Level = log.INFO
		item.Tag = log.DefaultTag
		item.Message = ".........................."
		err = store.Store(&item)
		if err != nil {
			break
		}
	}
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("Write success.")
}