1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
kratos/log/filter_test.go

68 lines
1.9 KiB
Go
Raw Normal View History

2021-06-14 00:14:32 +08:00
package log
import (
"testing"
)
func TestFilterAll(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger,
2021-06-14 00:14:32 +08:00
FilterLevel(LevelDebug),
2021-06-14 01:11:24 +08:00
FilterKeys("username"),
FilterValues("hello"),
FilterHook(testFilterFunc),
))
2021-06-14 00:14:32 +08:00
log.Log(LevelDebug, "msg", "test debug")
log.Info("hello")
log.Infow("password", "123456")
2021-06-14 00:54:48 +08:00
log.Infow("username", "kratos")
2021-06-14 00:14:32 +08:00
log.Warn("warn log")
}
func TestFilterLevel(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(NewFilter(logger, FilterLevel(LevelWarn))))
2021-06-14 00:14:32 +08:00
log.Log(LevelDebug, "msg1", "te1st debug")
log.Debug("test debug")
log.Debugf("test %s", "debug")
log.Debugw("log", "test debug")
log.Warn("warn log")
}
2021-06-14 01:11:24 +08:00
func TestFilterCaller(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewFilter(NewFilter(logger))
log.Log(LevelDebug, "msg1", "te1st debug")
logHelper := NewHelper(NewFilter(NewFilter(logger)))
logHelper.Log(LevelDebug, "msg1", "te1st debug")
}
2021-06-14 00:14:32 +08:00
func TestFilterKey(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 01:11:24 +08:00
log := NewHelper(NewFilter(logger, FilterKeys("password")))
2021-06-14 00:14:32 +08:00
log.Debugw("password", "123456")
}
func TestFilterValue(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 01:11:24 +08:00
log := NewHelper(NewFilter(logger, FilterValues("debug")))
2021-06-14 00:14:32 +08:00
log.Debugf("test %s", "debug")
}
func TestFilterFunc(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 01:11:24 +08:00
log := NewHelper(NewFilter(logger, FilterHook(testFilterFunc)))
2021-06-14 00:14:32 +08:00
log.Debug("debug level")
log.Infow("password", "123456")
}
func testFilterFunc(level Level, keyvals ...interface{}) bool {
if level == LevelWarn {
return true
}
for i := 0; i < len(keyvals); i++ {
if keyvals[i] == "password" {
keyvals[i+1] = "***"
}
}
return false
2021-06-14 00:54:48 +08:00
}