mirror of
https://github.com/astaxie/beego.git
synced 2024-10-05 05:21:48 +00:00
8982f5d702
Also moved is Colorful check to WriteMsg function to make the interface for user's using the custom logging formatting simpler. The user does not have to check if the text is colorful now, the WriteMsg function handles it.
37 lines
895 B
Go
37 lines
895 B
Go
package logformattertest
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/astaxie/beego/pkg/common"
|
|
"github.com/astaxie/beego/pkg/logs"
|
|
)
|
|
|
|
func customFormatter(lm *logs.LogMsg) string {
|
|
return fmt.Sprintf("[CUSTOM CONSOLE LOGGING] %s", lm.Msg)
|
|
}
|
|
|
|
func globalFormatter(lm *logs.LogMsg) string {
|
|
return fmt.Sprintf("[GLOBAL] %s", lm.Msg)
|
|
}
|
|
|
|
func TestCustomLoggingFormatter(t *testing.T) {
|
|
// beego.BConfig.Log.AccessLogs = true
|
|
|
|
logs.SetLoggerWithOpts("console", []string{`{"color":true}`}, common.SimpleKV{Key: "formatter", Value: customFormatter})
|
|
|
|
// Message will be formatted by the customFormatter with colorful text set to true
|
|
logs.Informational("Test message")
|
|
}
|
|
|
|
func TestGlobalLoggingFormatter(t *testing.T) {
|
|
logs.SetGlobalFormatter(globalFormatter)
|
|
|
|
logs.SetLogger("console", `{"color":true}`)
|
|
|
|
// Message will be formatted by globalFormatter
|
|
logs.Informational("Test message")
|
|
|
|
}
|