add es log
This commit is contained in:
@@ -43,9 +43,11 @@ type Config struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
} `yaml:"redis"`
|
||||
Elasticsearch struct{
|
||||
Host string `yaml:"host"`
|
||||
Index string `yaml:"index"`
|
||||
Elasticsearch struct {
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
Host string `yaml:"host"`
|
||||
Index string `yaml:"index"`
|
||||
} `yaml:"elasticsearch"`
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package linemsg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.trj.tw/golang/mtfosbot/model"
|
||||
"git.trj.tw/golang/mtfosbot/module/apis/line"
|
||||
"git.trj.tw/golang/mtfosbot/module/config"
|
||||
"git.trj.tw/golang/mtfosbot/module/es"
|
||||
lineobj "git.trj.tw/golang/mtfosbot/module/line-message/line-object"
|
||||
msgcmd "git.trj.tw/golang/mtfosbot/module/message-command"
|
||||
)
|
||||
@@ -91,6 +94,7 @@ func saveTextMsgToLog(txt string, s *lineobj.SourceObject) {
|
||||
return
|
||||
}
|
||||
|
||||
go saveLineMessageLogToES(s.GroupID, s.UserID, txt, "text")
|
||||
model.AddLineMessageLog(s.GroupID, s.UserID, txt, "text")
|
||||
}
|
||||
|
||||
@@ -137,5 +141,41 @@ func saveImageMsgToLog(id string, s *lineobj.SourceObject) {
|
||||
return
|
||||
}
|
||||
|
||||
furl, err := url.Parse(conf.URL)
|
||||
if err == nil {
|
||||
furl, err = furl.Parse(fmt.Sprintf("/image/line_log_image/%s", fname))
|
||||
if err == nil {
|
||||
fname = furl.String()
|
||||
}
|
||||
}
|
||||
|
||||
go saveLineMessageLogToES(s.GroupID, s.UserID, fname, "image")
|
||||
model.AddLineMessageLog(s.GroupID, s.UserID, fname, "image")
|
||||
}
|
||||
|
||||
func saveLineMessageLogToES(gid, uid, content, msgType string) {
|
||||
lineGroup, err := model.GetLineGroup(gid)
|
||||
if err != nil {
|
||||
log.Println("get line group error :: ", err)
|
||||
return
|
||||
}
|
||||
lineUser, err := model.GetLineUserByID(uid)
|
||||
if err != nil {
|
||||
log.Println("get line user error :: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
logMsg := make(map[string]interface{})
|
||||
|
||||
logMsg["message"] = content
|
||||
logMsg["type"] = msgType
|
||||
logMsg["group"] = lineGroup.ID
|
||||
logMsg["group_name"] = lineGroup.Name
|
||||
logMsg["user"] = lineUser.ID
|
||||
logMsg["user_name"] = lineUser.Name
|
||||
|
||||
err = es.PutLog("log", logMsg)
|
||||
if err != nil {
|
||||
log.Println("put log fail :: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user