You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/conf/logger_zinc.go

72 lines
1.4 KiB

package conf
import (
"fmt"
"time"
"github.com/rocboss/paopao-ce/pkg/json"
"github.com/sirupsen/logrus"
"gopkg.in/resty.v1"
)
type zincLogData struct {
Time time.Time `json:"time"`
Level logrus.Level `json:"level"`
Message string `json:"message"`
Data logrus.Fields `json:"data"`
}
type zincLogIndex struct {
Index map[string]string `json:"index"`
}
type zincLogHook struct {
host string
index string
user string
password string
}
func (h *zincLogHook) Fire(entry *logrus.Entry) error {
index := &zincLogIndex{
Index: map[string]string{
"_index": h.index,
},
}
indexBytes, _ := json.Marshal(index)
data := &zincLogData{
Time: entry.Time,
Level: entry.Level,
Message: entry.Message,
Data: entry.Data,
}
dataBytes, _ := json.Marshal(data)
logStr := string(indexBytes) + "\n" + string(dataBytes) + "\n"
client := resty.New()
if _, err := client.SetDisableWarn(true).R().
SetHeader("Content-Type", "application/json").
SetBasicAuth(h.user, h.password).
SetBody(logStr).
Post(h.host); err != nil {
fmt.Println(err.Error())
}
return nil
}
func (h *zincLogHook) Levels() []logrus.Level {
return logrus.AllLevels
}
func newZincLogHook() *zincLogHook {
return &zincLogHook{
host: loggerZincSetting.Endpoint() + "/es/_bulk",
index: loggerZincSetting.Index,
user: loggerZincSetting.User,
password: loggerZincSetting.Password,
}
}