1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/log/filter_test.go

91 lines
2.5 KiB
Go
Raw Normal View History

2021-06-13 18:14:32 +02:00
package log
import (
2021-06-14 01:50:44 +02:00
"io/ioutil"
2021-06-13 18:14:32 +02:00
"testing"
)
func TestFilterAll(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger,
2021-06-13 18:14:32 +02:00
FilterLevel(LevelDebug),
2021-06-14 04:14:23 +02:00
FilterKey("username"),
FilterValue("hello"),
FilterFunc(testFilterFunc),
))
2021-06-13 18:14:32 +02:00
log.Log(LevelDebug, "msg", "test debug")
log.Info("hello")
log.Infow("password", "123456")
2021-06-13 18:54:48 +02:00
log.Infow("username", "kratos")
2021-06-13 18:14:32 +02: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-13 18:14:32 +02: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-13 19:11:24 +02:00
func TestFilterCaller(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-13 19:47:48 +02:00
log := NewFilter(logger)
2021-06-13 19:11:24 +02:00
log.Log(LevelDebug, "msg1", "te1st debug")
2021-06-14 01:50:44 +02:00
logHelper := NewHelper(NewFilter(logger))
2021-06-13 19:11:24 +02:00
logHelper.Log(LevelDebug, "msg1", "te1st debug")
}
2021-06-13 18:14:32 +02:00
func TestFilterKey(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 04:14:23 +02:00
log := NewHelper(NewFilter(logger, FilterKey("password")))
2021-06-13 18:14:32 +02:00
log.Debugw("password", "123456")
}
func TestFilterValue(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 04:14:23 +02:00
log := NewHelper(NewFilter(logger, FilterValue("debug")))
2021-06-13 18:14:32 +02:00
log.Debugf("test %s", "debug")
}
func TestFilterFunc(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
2021-06-14 04:14:23 +02:00
log := NewHelper(NewFilter(logger, FilterFunc(testFilterFunc)))
2021-06-13 18:14:32 +02:00
log.Debug("debug level")
log.Infow("password", "123456")
}
2021-06-14 04:14:23 +02:00
func BenchmarkFilterKey(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterKey("password")))
2021-06-14 01:50:44 +02:00
for i := 0; i < b.N; i++ {
log.Infow("password", "123456")
}
}
2021-06-14 04:14:23 +02:00
func BenchmarkFilterValue(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterValue("password")))
2021-06-14 01:50:44 +02:00
for i := 0; i < b.N; i++ {
log.Infow("password")
}
}
2021-06-14 04:14:23 +02:00
func BenchmarkFilterFunc(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterFunc(testFilterFunc)))
2021-06-14 01:50:44 +02:00
for i := 0; i < b.N; i++ {
2021-06-14 04:14:23 +02:00
log.Info("password", "123456")
2021-06-14 01:50:44 +02:00
}
}
2021-06-13 18:14:32 +02: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] = "***"
}
}
return false
2021-06-13 19:47:48 +02:00
}