1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 11:10:57 +00:00
Beego/core/logs/formatter_test.go

96 lines
2.2 KiB
Go
Raw Normal View History

2020-09-19 15:04:40 +00:00
// 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 logs
import (
2020-09-19 15:04:40 +00:00
"encoding/json"
"errors"
"strconv"
"testing"
"time"
2020-09-19 15:04:40 +00:00
"github.com/stretchr/testify/assert"
)
2020-09-19 15:04:40 +00:00
type CustomFormatter struct{}
func (c *CustomFormatter) Format(lm *LogMsg) string {
return "hello, msg: " + lm.Msg
}
type TestLogger struct {
Formatter string `json:"formatter"`
Expected string
formatter LogFormatter
}
func (t *TestLogger) Init(config string) error {
er := json.Unmarshal([]byte(config), t)
t.formatter, _ = GetFormatter(t.Formatter)
return er
}
func (t *TestLogger) WriteMsg(lm *LogMsg) error {
msg := t.formatter.Format(lm)
if msg != t.Expected {
return errors.New("not equal")
}
return nil
}
func (t *TestLogger) Destroy() {
panic("implement me")
}
func (t *TestLogger) Flush() {
panic("implement me")
}
func (t *TestLogger) SetFormatter(f LogFormatter) {
panic("implement me")
}
func TestCustomFormatter(t *testing.T) {
RegisterFormatter("custom", &CustomFormatter{})
tl := &TestLogger{
Expected: "hello, msg: world",
}
assert.Nil(t, tl.Init(`{"formatter": "custom"}`))
assert.Nil(t, tl.WriteMsg(&LogMsg{
Msg: "world",
}))
}
func TestPatternLogFormatter(t *testing.T) {
2020-09-19 12:18:09 +00:00
tes := &PatternLogFormatter{
Pattern: "%F:%n|%w%t>> %m",
WhenFormat: "2006-01-02",
}
when := time.Now()
lm := &LogMsg{
Msg: "message",
FilePath: "/User/go/beego/main.go",
Level: LevelWarn,
LineNumber: 10,
When: when,
}
got := tes.ToString(lm)
want := lm.FilePath + ":" + strconv.Itoa(lm.LineNumber) + "|" +
when.Format(tes.WhenFormat) + levelPrefix[lm.Level-1] + ">> " + lm.Msg
if got != want {
t.Errorf("want %s, got %s", want, got)
}
2020-09-20 05:15:38 +00:00
}