You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/log/filter_test.go

92 lines
2.5 KiB

3 years ago
package log
import (
"io"
3 years ago
"testing"
)
func TestFilterAll(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger,
3 years ago
FilterLevel(LevelDebug),
3 years ago
FilterKey("username"),
FilterValue("hello"),
FilterFunc(testFilterFunc),
))
3 years ago
log.Log(LevelDebug, "msg", "test debug")
log.Info("hello")
log.Infow("password", "123456")
log.Infow("username", "kratos")
3 years ago
log.Warn("warn log")
}
3 years ago
func TestFilterLevel(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(NewFilter(logger, FilterLevel(LevelWarn))))
3 years ago
log.Log(LevelDebug, "msg1", "te1st debug")
log.Debug("test debug")
log.Debugf("test %s", "debug")
log.Debugw("log", "test debug")
log.Warn("warn log")
}
3 years ago
func TestFilterCaller(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewFilter(logger)
_ = log.Log(LevelDebug, "msg1", "te1st debug")
logHelper := NewHelper(NewFilter(logger))
3 years ago
logHelper.Log(LevelDebug, "msg1", "te1st debug")
}
3 years ago
func TestFilterKey(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
3 years ago
log := NewHelper(NewFilter(logger, FilterKey("password")))
3 years ago
log.Debugw("password", "123456")
}
func TestFilterValue(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
3 years ago
log := NewHelper(NewFilter(logger, FilterValue("debug")))
3 years ago
log.Debugf("test %s", "debug")
}
func TestFilterFunc(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
3 years ago
log := NewHelper(NewFilter(logger, FilterFunc(testFilterFunc)))
3 years ago
log.Debug("debug level")
log.Infow("password", "123456")
}
3 years ago
func BenchmarkFilterKey(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterKey("password")))
for i := 0; i < b.N; i++ {
log.Infow("password", "123456")
}
}
3 years ago
func BenchmarkFilterValue(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterValue("password")))
for i := 0; i < b.N; i++ {
log.Infow("password")
}
}
3 years ago
func BenchmarkFilterFunc(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterFunc(testFilterFunc)))
for i := 0; i < b.N; i++ {
3 years ago
log.Info("password", "123456")
}
}
3 years ago
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
3 years ago
}
}
return false
}