diff --git a/pkg/infrastructure/logs/es/es.go b/pkg/infrastructure/logs/es/es.go index 438a6da6..c4090eab 100644 --- a/pkg/infrastructure/logs/es/es.go +++ b/pkg/infrastructure/logs/es/es.go @@ -18,7 +18,8 @@ import ( // NewES returns a LoggerInterface func NewES() logs.Logger { cw := &esLogger{ - Level: logs.LevelDebug, + 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), } diff --git a/pkg/infrastructure/logs/es/index.go b/pkg/infrastructure/logs/es/index.go new file mode 100644 index 00000000..9796987e --- /dev/null +++ b/pkg/infrastructure/logs/es/index.go @@ -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()) +} diff --git a/pkg/infrastructure/logs/es/index_test.go b/pkg/infrastructure/logs/es/index_test.go new file mode 100644 index 00000000..4cdf9b02 --- /dev/null +++ b/pkg/infrastructure/logs/es/index_test.go @@ -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) +} diff --git a/pkg/infrastructure/logs/formatter_test.go b/pkg/infrastructure/logs/formatter_test.go index 7ba9237b..a97765ac 100644 --- a/pkg/infrastructure/logs/formatter_test.go +++ b/pkg/infrastructure/logs/formatter_test.go @@ -92,4 +92,4 @@ func TestPatternLogFormatter(t *testing.T) { if got != want { t.Errorf("want %s, got %s", want, got) } -} \ No newline at end of file +}