1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 12:01:28 +00:00
Beego/logs/es/es.go

103 lines
2.0 KiB
Go
Raw Normal View History

2015-06-12 16:25:48 +00:00
package es
import (
2020-06-06 12:14:01 +00:00
"context"
2015-06-12 16:25:48 +00:00
"encoding/json"
"errors"
"fmt"
"net/url"
2020-06-06 12:14:01 +00:00
"strings"
2015-06-12 16:25:48 +00:00
"time"
2020-06-06 12:14:01 +00:00
"github.com/elastic/go-elasticsearch/v6"
"github.com/elastic/go-elasticsearch/v6/esapi"
2019-01-02 09:01:46 +00:00
"github.com/astaxie/beego/logs"
2015-06-12 16:25:48 +00:00
)
2015-09-11 15:08:24 +00:00
// NewES return a LoggerInterface
func NewES() logs.Logger {
2015-06-12 16:25:48 +00:00
cw := &esLogger{
Level: logs.LevelDebug,
}
return cw
}
2020-06-29 13:53:52 +00:00
// 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:
2020-06-30 02:12:10 +00:00
// import _ "github.com/astaxie/beego/logs/es"
2015-06-12 16:25:48 +00:00
type esLogger struct {
2020-06-06 12:14:01 +00:00
*elasticsearch.Client
2015-06-12 16:25:48 +00:00
DSN string `json:"dsn"`
Level int `json:"level"`
}
// {"dsn":"http://localhost:9200/","level":1}
func (el *esLogger) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), el)
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 {
2020-06-06 12:14:01 +00:00
conn, err := elasticsearch.NewClient(elasticsearch.Config{
2020-06-29 13:53:52 +00:00
Addresses: []string{el.DSN},
2020-06-06 12:14:01 +00:00
})
if err != nil {
return err
}
2019-01-02 09:01:46 +00:00
el.Client = conn
2015-06-12 16:25:48 +00:00
}
return nil
}
2015-09-11 15:08:24 +00:00
// WriteMsg will write the msg and level into es
2016-01-23 08:24:58 +00:00
func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
2015-06-12 16:25:48 +00:00
if level > el.Level {
return nil
}
2016-01-23 08:24:58 +00:00
2020-06-06 12:14:01 +00:00
idx := LogDocument{
2020-06-28 13:12:12 +00:00
Timestamp: when.Format(time.RFC3339),
Msg: msg,
2020-06-06 12:14:01 +00:00
}
body, err := json.Marshal(idx)
if err != nil {
return err
2015-06-12 16:25:48 +00:00
}
2020-06-06 12:14:01 +00:00
req := esapi.IndexRequest{
Index: fmt.Sprintf("%04d.%02d.%02d", when.Year(), when.Month(), when.Day()),
DocumentType: "logs",
Body: strings.NewReader(string(body)),
}
_, err = req.Do(context.Background(), el.Client)
2015-06-12 16:25:48 +00:00
return err
}
2015-09-11 15:08:24 +00:00
// Destroy is a empty method
2015-06-12 16:25:48 +00:00
func (el *esLogger) Destroy() {
}
2015-09-11 15:08:24 +00:00
// Flush is a empty method
2015-06-12 16:25:48 +00:00
func (el *esLogger) Flush() {
}
2020-06-06 12:14:01 +00:00
type LogDocument struct {
2020-06-28 13:12:12 +00:00
Timestamp string `json:"timestamp"`
Msg string `json:"msg"`
2020-06-06 12:14:01 +00:00
}
2015-06-12 16:25:48 +00:00
func init() {
2016-03-24 10:21:52 +00:00
logs.Register(logs.AdapterEs, NewES)
2015-06-12 16:25:48 +00:00
}