1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 18:10:55 +00:00

Add init to es.go

This commit is contained in:
IamCathal 2020-08-24 20:41:39 +01:00
parent 48a98ec1a5
commit c5970766a3

View File

@ -33,6 +33,8 @@ type esLogger struct {
*elasticsearch.Client
DSN string `json:"dsn"`
Level int `json:"level"`
UseCustomFormatter bool
CustomFormatter func(*logs.LogMsg) string
}
func (el *esLogger) Format(lm *logs.LogMsg) string {
@ -40,7 +42,14 @@ func (el *esLogger) Format(lm *logs.LogMsg) string {
}
// {"dsn":"http://localhost:9200/","level":1}
func (el *esLogger) Init(jsonconfig string) error {
func (el *esLogger) Init(jsonconfig string, LogFormatter ...func(*logs.LogMsg) string) error {
for _, elem := range LogFormatter {
if elem != nil {
el.UseCustomFormatter = true
el.CustomFormatter = elem
}
}
err := json.Unmarshal([]byte(jsonconfig), el)
if err != nil {
return err
@ -69,9 +78,16 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
return nil
}
msg := ""
if el.UseCustomFormatter {
msg = el.CustomFormatter(lm)
} else {
msg = el.Format(lm)
}
idx := LogDocument{
Timestamp: lm.When.Format(time.RFC3339),
Msg: el.Format(lm),
Msg: msg,
}
body, err := json.Marshal(idx)