mirror of
https://github.com/astaxie/beego.git
synced 2024-11-06 06:20:55 +00:00
Merge pull request #4233 from flycash/esIndexName
Add IndexNaming interface
This commit is contained in:
commit
7effdb0e7d
@ -18,7 +18,8 @@ import (
|
|||||||
// NewES returns a LoggerInterface
|
// NewES returns a LoggerInterface
|
||||||
func NewES() logs.Logger {
|
func NewES() logs.Logger {
|
||||||
cw := &esLogger{
|
cw := &esLogger{
|
||||||
Level: logs.LevelDebug,
|
Level: logs.LevelDebug,
|
||||||
|
indexNaming: indexNaming,
|
||||||
}
|
}
|
||||||
return cw
|
return cw
|
||||||
}
|
}
|
||||||
@ -35,6 +36,8 @@ type esLogger struct {
|
|||||||
Level int `json:"level"`
|
Level int `json:"level"`
|
||||||
formatter logs.LogFormatter
|
formatter logs.LogFormatter
|
||||||
Formatter string `json:"formatter"`
|
Formatter string `json:"formatter"`
|
||||||
|
|
||||||
|
indexNaming IndexNaming
|
||||||
}
|
}
|
||||||
|
|
||||||
func (el *esLogger) Format(lm *logs.LogMsg) string {
|
func (el *esLogger) Format(lm *logs.LogMsg) string {
|
||||||
@ -96,7 +99,7 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
|
|||||||
msg := el.formatter.Format(lm)
|
msg := el.formatter.Format(lm)
|
||||||
|
|
||||||
req := esapi.IndexRequest{
|
req := esapi.IndexRequest{
|
||||||
Index: fmt.Sprintf("%04d.%02d.%02d", lm.When.Year(), lm.When.Month(), lm.When.Day()),
|
Index: indexNaming.IndexName(lm),
|
||||||
DocumentType: "logs",
|
DocumentType: "logs",
|
||||||
Body: strings.NewReader(msg),
|
Body: strings.NewReader(msg),
|
||||||
}
|
}
|
||||||
|
39
pkg/infrastructure/logs/es/index.go
Normal file
39
pkg/infrastructure/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/pkg/infrastructure/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
pkg/infrastructure/logs/es/index_test.go
Normal file
34
pkg/infrastructure/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/pkg/infrastructure/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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user