log_formatter_test.go 825 字节
Newer Older
M
Ming Deng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
package logs

import (
	"fmt"
	"testing"

	"github.com/astaxie/beego/pkg/infrastructure/utils"
)

func customFormatter(lm *LogMsg) string {
	return fmt.Sprintf("[CUSTOM CONSOLE LOGGING] %s", lm.Msg)
}

func globalFormatter(lm *LogMsg) string {
	return fmt.Sprintf("[GLOBAL] %s", lm.Msg)
}

func TestCustomLoggingFormatter(t *testing.T) {
	// beego.BConfig.Log.AccessLogs = true

	SetLoggerWithOpts("console", []string{`{"color":true}`}, &utils.SimpleKV{Key: "formatter", Value: customFormatter})

	// Message will be formatted by the customFormatter with colorful text set to true
	Informational("Test message")
}

func TestGlobalLoggingFormatter(t *testing.T) {
	SetGlobalFormatter(globalFormatter)

	SetLogger("console", `{"color":true}`)

	// Message will be formatted by globalFormatter
	Informational("Test message")

}