1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 16:50:49 +00:00
Beego/pkg/infrastructure/logs/es/es.go

124 lines
2.5 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
package es
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v6"
"github.com/elastic/go-elasticsearch/v6/esapi"
"github.com/astaxie/beego/pkg/infrastructure/logs"
2020-07-22 14:50:08 +00:00
)
2020-08-06 15:07:18 +00:00
// NewES returns a LoggerInterface
2020-07-22 14:50:08 +00:00
func NewES() logs.Logger {
cw := &esLogger{
Level: logs.LevelDebug,
}
return cw
}
// esLogger will log msg into ES
// before you using this implementation,
// please import this package
// usually means that you can import this package in your main package
// for example, anonymous:
// import _ "github.com/astaxie/beego/logs/es"
type esLogger struct {
*elasticsearch.Client
2020-09-11 13:10:12 +00:00
DSN string `json:"dsn"`
Level int `json:"level"`
formatter logs.LogFormatter
Formatter string `json:"formatter"`
2020-07-22 14:50:08 +00:00
}
2020-08-20 18:00:35 +00:00
func (el *esLogger) Format(lm *logs.LogMsg) string {
2020-09-11 13:10:12 +00:00
msg := lm.OldStyleFormat()
idx := LogDocument{
Timestamp: lm.When.Format(time.RFC3339),
Msg: msg,
}
body, err := json.Marshal(idx)
if err != nil {
return msg
}
return string(body)
}
func (el *esLogger) SetFormatter(f logs.LogFormatter) {
el.formatter = f
2020-08-20 18:00:35 +00:00
}
2020-07-22 14:50:08 +00:00
// {"dsn":"http://localhost:9200/","level":1}
2020-09-11 13:10:12 +00:00
func (el *esLogger) Init(config string) error {
2020-08-24 19:41:39 +00:00
2020-09-11 13:10:12 +00:00
err := json.Unmarshal([]byte(config), el)
2020-07-22 14:50:08 +00:00
if err != nil {
return err
}
if el.DSN == "" {
return errors.New("empty dsn")
} else if u, err := url.Parse(el.DSN); err != nil {
return err
} else if u.Path == "" {
return errors.New("missing prefix")
} else {
conn, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{el.DSN},
})
if err != nil {
return err
}
el.Client = conn
}
2020-09-11 13:10:12 +00:00
if len(el.Formatter) > 0 {
fmtr, ok := logs.GetFormatter(el.Formatter)
if !ok {
return errors.New(fmt.Sprintf("the formatter with name: %s not found", el.Formatter))
}
el.formatter = fmtr
}
2020-07-22 14:50:08 +00:00
return nil
}
2020-08-06 15:07:18 +00:00
// WriteMsg writes the msg and level into es
2020-08-19 15:13:42 +00:00
func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
if lm.Level > el.Level {
2020-07-22 14:50:08 +00:00
return nil
}
2020-09-11 13:10:12 +00:00
msg := el.formatter.Format(lm)
2020-07-22 14:50:08 +00:00
req := esapi.IndexRequest{
2020-08-19 15:13:42 +00:00
Index: fmt.Sprintf("%04d.%02d.%02d", lm.When.Year(), lm.When.Month(), lm.When.Day()),
2020-07-22 14:50:08 +00:00
DocumentType: "logs",
2020-09-11 13:10:12 +00:00
Body: strings.NewReader(msg),
2020-07-22 14:50:08 +00:00
}
2020-09-11 13:10:12 +00:00
_, err := req.Do(context.Background(), el.Client)
2020-07-22 14:50:08 +00:00
return err
}
// Destroy is a empty method
func (el *esLogger) Destroy() {
}
// Flush is a empty method
func (el *esLogger) Flush() {
}
type LogDocument struct {
Timestamp string `json:"timestamp"`
Msg string `json:"msg"`
}
func init() {
logs.Register(logs.AdapterEs, NewES)
}