1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-30 04:31:03 +02:00
kratos/log/filter_test.go

92 lines
2.5 KiB
Go
Raw Normal View History

2021-06-14 00:14:32 +08:00
package log
import (
"io"
2021-06-14 00:14:32 +08:00
"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 10:14:23 +08:00
FilterKey("username"),
FilterValue("hello"),
FilterFunc(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")
}
2021-06-14 00:14:32 +08:00
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)
2021-06-14 01:47:48 +08:00
log := NewFilter(logger)
_ = log.Log(LevelDebug, "msg1", "te1st debug")
2021-06-14 07:50:44 +08:00
logHelper := NewHelper(NewFilter(logger))
2021-06-14 01:11:24 +08:00
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 10:14:23 +08:00
log := NewHelper(NewFilter(logger, FilterKey("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 10:14:23 +08:00
log := NewHelper(NewFilter(logger, FilterValue("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 10:14:23 +08:00
log := NewHelper(NewFilter(logger, FilterFunc(testFilterFunc)))
2021-06-14 00:14:32 +08:00
log.Debug("debug level")
log.Infow("password", "123456")
}
2021-06-14 10:14:23 +08:00
func BenchmarkFilterKey(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterKey("password")))
2021-06-14 07:50:44 +08:00
for i := 0; i < b.N; i++ {
log.Infow("password", "123456")
}
}
2021-06-14 10:14:23 +08:00
func BenchmarkFilterValue(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterValue("password")))
2021-06-14 07:50:44 +08:00
for i := 0; i < b.N; i++ {
log.Infow("password")
}
}
2021-06-14 10:14:23 +08:00
func BenchmarkFilterFunc(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterFunc(testFilterFunc)))
2021-06-14 07:50:44 +08:00
for i := 0; i < b.N; i++ {
2021-06-14 10:14:23 +08:00
log.Info("password", "123456")
2021-06-14 07:50:44 +08:00
}
}
2021-06-14 00:14:32 +08:00
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] = fuzzyStr
2021-06-14 00:14:32 +08:00
}
}
return false
2021-06-14 01:47:48 +08:00
}