mirror of
https://github.com/astaxie/beego.git
synced 2025-06-15 08:00:39 +00:00
remove pkg directory;
remove build directory; remove githook directory;
This commit is contained in:
126
core/logs/es/es.go
Normal file
126
core/logs/es/es.go
Normal file
@ -0,0 +1,126 @@
|
||||
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/core/logs"
|
||||
)
|
||||
|
||||
// NewES returns a LoggerInterface
|
||||
func NewES() logs.Logger {
|
||||
cw := &esLogger{
|
||||
Level: logs.LevelDebug,
|
||||
indexNaming: indexNaming,
|
||||
}
|
||||
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
|
||||
DSN string `json:"dsn"`
|
||||
Level int `json:"level"`
|
||||
formatter logs.LogFormatter
|
||||
Formatter string `json:"formatter"`
|
||||
|
||||
indexNaming IndexNaming
|
||||
}
|
||||
|
||||
func (el *esLogger) Format(lm *logs.LogMsg) string {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// {"dsn":"http://localhost:9200/","level":1}
|
||||
func (el *esLogger) Init(config string) error {
|
||||
|
||||
err := json.Unmarshal([]byte(config), 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 {
|
||||
conn, err := elasticsearch.NewClient(elasticsearch.Config{
|
||||
Addresses: []string{el.DSN},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
el.Client = conn
|
||||
}
|
||||
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
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteMsg writes the msg and level into es
|
||||
func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
|
||||
if lm.Level > el.Level {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := el.formatter.Format(lm)
|
||||
|
||||
req := esapi.IndexRequest{
|
||||
Index: indexNaming.IndexName(lm),
|
||||
DocumentType: "logs",
|
||||
Body: strings.NewReader(msg),
|
||||
}
|
||||
_, err := req.Do(context.Background(), el.Client)
|
||||
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)
|
||||
}
|
39
core/logs/es/index.go
Normal file
39
core/logs/es/index.go
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2020
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package es
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/astaxie/beego/core/logs"
|
||||
)
|
||||
|
||||
// IndexNaming generate the index name
|
||||
type IndexNaming interface {
|
||||
IndexName(lm *logs.LogMsg) string
|
||||
}
|
||||
|
||||
var indexNaming IndexNaming = &defaultIndexNaming{}
|
||||
|
||||
// SetIndexNaming will register global IndexNaming
|
||||
func SetIndexNaming(i IndexNaming) {
|
||||
indexNaming = i
|
||||
}
|
||||
|
||||
type defaultIndexNaming struct{}
|
||||
|
||||
func (d *defaultIndexNaming) IndexName(lm *logs.LogMsg) string {
|
||||
return fmt.Sprintf("%04d.%02d.%02d", lm.When.Year(), lm.When.Month(), lm.When.Day())
|
||||
}
|
34
core/logs/es/index_test.go
Normal file
34
core/logs/es/index_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright 2020
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package es
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/core/logs"
|
||||
)
|
||||
|
||||
func TestDefaultIndexNaming_IndexName(t *testing.T) {
|
||||
tm := time.Date(2020, 9, 12, 1, 34, 45, 234, time.UTC)
|
||||
lm := &logs.LogMsg{
|
||||
When: tm,
|
||||
}
|
||||
|
||||
res := (&defaultIndexNaming{}).IndexName(lm)
|
||||
assert.Equal(t, "2020.09.12", res)
|
||||
}
|
Reference in New Issue
Block a user