make the feature of LoggerMeili work well

pull/129/head
alimy 3 years ago
parent 4a6eb41a36
commit 6f27d95856

@ -302,7 +302,7 @@ release/paopao-ce --no-default-features --features sqlite3,localoss,loggerfile,r
* 日志: LoggerFile/LoggerZinc/LoggerMeili
`LoggerFile` 使用文件写日志(目前状态: 稳定);
`LoggerZinc` 使用[Zinc](https://github.com/zinclabs/zinc)写日志(目前状态: 稳定,推荐使用);
`LoggerMeili` 使用[Meilisearch](https://github.com/meilisearch/meilisearch)写日志(目前状态: 调试阶段);
`LoggerMeili` 使用[Meilisearch](https://github.com/meilisearch/meilisearch)写日志(目前状态: 内测阶段);
* 支付: Alipay
* 短信验证码: SmsJuhe(需要开启sms)
`Sms`功能如果没有开启,任意短信验证码都可以绑定手机;

@ -53,6 +53,8 @@ LoggerMeili: # 使用Meili写日志
Index: paopao-log
ApiKey: paopao-meilisearch
Secure: False
MinWorker: 5 # 最小后台工作者, 设置范围[5, 100], 默认5
MaxLogBuffer: 100 # 最大log缓存条数, 设置范围[10, 10000], 默认100
JWT: # 鉴权加密
Secret: 18a6413dc4fe394c66345ebe501b2f26
Issuer: paopao-api

@ -1,118 +1,12 @@
package conf
import (
"fmt"
"io"
"time"
"github.com/meilisearch/meilisearch-go"
"github.com/rocboss/paopao-ce/pkg/json"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/resty.v1"
)
type logData 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
}
type meiliLogHook struct {
index *meilisearch.Index
}
func (h *zincLogHook) Fire(entry *logrus.Entry) error {
index := &zincLogIndex{
Index: map[string]string{
"_index": h.index,
},
}
indexBytes, _ := json.Marshal(index)
data := &logData{
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 (h *meiliLogHook) Fire(entry *logrus.Entry) error {
data := &logData{
Time: entry.Time,
Level: entry.Level,
Message: entry.Message,
Data: entry.Data,
}
if _, err := h.index.AddDocuments(data); err != nil {
fmt.Println(err.Error())
}
return nil
}
func (h *meiliLogHook) 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,
}
}
func newMeiliLogHook() *meiliLogHook {
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: loggerMeiliSetting.Endpoint(),
APIKey: loggerMeiliSetting.ApiKey,
})
index := client.Index(loggerMeiliSetting.Index)
if _, err := index.FetchInfo(); err != nil {
logrus.Debugf("newMeiliLogHook create index because fetch index info error: %v", err)
client.CreateIndex(&meilisearch.IndexConfig{
Uid: loggerMeiliSetting.Index,
})
}
return &meiliLogHook{
index: index,
}
}
func newFileLogger() io.Writer {
return &lumberjack.Logger{
Filename: loggerFileSetting.SavePath + "/" + loggerFileSetting.FileName + loggerFileSetting.FileExt,

@ -0,0 +1,77 @@
package conf
import (
"github.com/meilisearch/meilisearch-go"
"github.com/sirupsen/logrus"
)
type meiliLogData []map[string]interface{}
type meiliLogHook struct {
config meilisearch.ClientConfig
idxName string
addDocsCh chan *meiliLogData
}
func (h *meiliLogHook) Fire(entry *logrus.Entry) error {
data := meiliLogData{{
"id": entry.Time.Unix(),
"time": entry.Time,
"level": entry.Level,
"message": entry.Message,
"data": entry.Data,
}}
// 先尝试进log缓存否则直接加文档
select {
case h.addDocsCh <- &data:
default:
h.index().AddDocuments(data)
}
return nil
}
func (h *meiliLogHook) handleAddDocs() {
index := h.index()
for item := range h.addDocsCh {
index.AddDocuments(item)
}
}
func (h *meiliLogHook) Levels() []logrus.Level {
return logrus.AllLevels
}
func (h *meiliLogHook) index() *meilisearch.Index {
return meilisearch.NewClient(h.config).Index(h.idxName)
}
func newMeiliLogHook() *meiliLogHook {
hook := &meiliLogHook{
config: meilisearch.ClientConfig{
Host: loggerMeiliSetting.Endpoint(),
APIKey: loggerMeiliSetting.ApiKey,
},
idxName: loggerMeiliSetting.Index,
}
client := meilisearch.NewClient(hook.config)
index := client.Index(hook.idxName)
if _, err := index.FetchInfo(); err != nil {
client.CreateIndex(&meilisearch.IndexConfig{
Uid: hook.idxName,
PrimaryKey: "id",
})
}
// 初始化addDocsCh
hook.addDocsCh = make(chan *meiliLogData, loggerMeiliSetting.maxLogBuffer())
// 启动后台log工作者
for minWork := loggerMeiliSetting.minWork(); minWork > 0; minWork-- {
go hook.handleAddDocs()
}
return hook
}

@ -0,0 +1,71 @@
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,
}
}

@ -33,10 +33,12 @@ type LoggerZincSettingS struct {
}
type LoggerMeiliSettingS struct {
Host string
Index string
ApiKey string
Secure bool
Host string
Index string
ApiKey string
Secure bool
MaxLogBuffer int
MinWorker int
}
type ServerSettingS struct {
@ -361,6 +363,24 @@ func (s *LoggerMeiliSettingS) Endpoint() string {
return endpoint(s.Host, s.Secure)
}
func (s *LoggerMeiliSettingS) minWork() int {
if s.MinWorker < 5 {
return 5
} else if s.MinWorker > 100 {
return 100
}
return s.MinWorker
}
func (s *LoggerMeiliSettingS) maxLogBuffer() int {
if s.MaxLogBuffer < 10 {
return 10
} else if s.MaxLogBuffer > 1000 {
return 1000
}
return s.MaxLogBuffer
}
func (s *ZincSettingS) Endpoint() string {
return endpoint(s.Host, s.Secure)
}

Loading…
Cancel
Save