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

Merge pull request #4233 from flycash/esIndexName

Add IndexNaming interface
This commit is contained in:
Ming Deng 2020-09-20 14:22:32 +08:00 committed by GitHub
commit 7effdb0e7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 2 deletions

View File

@ -19,6 +19,7 @@ import (
func NewES() logs.Logger {
cw := &esLogger{
Level: logs.LevelDebug,
indexNaming: indexNaming,
}
return cw
}
@ -35,6 +36,8 @@ type esLogger struct {
Level int `json:"level"`
formatter logs.LogFormatter
Formatter string `json:"formatter"`
indexNaming IndexNaming
}
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)
req := esapi.IndexRequest{
Index: fmt.Sprintf("%04d.%02d.%02d", lm.When.Year(), lm.When.Month(), lm.When.Day()),
Index: indexNaming.IndexName(lm),
DocumentType: "logs",
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)
}