1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-06 05:20:54 +00:00

Add IndexNaming interface so users can custom the index name when they use es as the logger

This commit is contained in:
Ming Deng 2020-09-19 23:49:41 +08:00
parent a1782cc22d
commit a3ece98cec
4 changed files with 79 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import (
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),
} }

View 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())
}

View 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)
}