add es log

This commit is contained in:
Jay
2019-05-28 11:17:22 +08:00
parent a7cb7cf8cf
commit 9ffbdfceca
7 changed files with 117 additions and 29 deletions
+8 -4
View File
@@ -14,7 +14,11 @@ var client *elastic.Client
// NewClient -
func NewClient() (err error) {
conf := config.GetConf()
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host))
client, err = elastic.NewClient(
elastic.SetSniff(false),
elastic.SetURL(conf.Elasticsearch.Host),
elastic.SetBasicAuth(conf.Elasticsearch.Username, conf.Elasticsearch.Password),
)
fmt.Println("host ", conf.Elasticsearch.Host)
if err != nil {
fmt.Println(err)
@@ -24,11 +28,11 @@ func NewClient() (err error) {
}
// PutLog -
func PutLog(t string, body map[string]interface{}) (err error) {
func PutLog(logType string, body map[string]interface{}) (err error) {
if client == nil {
return
}
if len(t) == 0 || body == nil {
if len(logType) == 0 || body == nil {
return
}
conf := config.GetConf()
@@ -37,6 +41,6 @@ func PutLog(t string, body map[string]interface{}) (err error) {
body["date"] = time.Now().UTC()
nt := time.Now()
idx := fmt.Sprintf("%v-%v-%v-%v", conf.Elasticsearch.Index, nt.Year(), int(nt.Month()), nt.Day())
_, err = client.Index().Index(idx).Type(t).BodyJson(body).Do(ctx)
_, err = client.Index().Index(idx).Type(logType).BodyJson(body).Do(ctx)
return
}