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

143 lines
3.7 KiB
Go
Raw Normal View History

2021-06-14 00:14:32 +08:00
package log
import (
"bytes"
"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
}
func TestFilterFuncWitchLoggerPrefix(t *testing.T) {
buf := new(bytes.Buffer)
tests := []struct {
logger Logger
want string
}{
{
logger: NewFilter(With(NewStdLogger(buf), "caller", "caller", "prefix", "whaterver"), FilterFunc(testFilterFuncWithLoggerPrefix)),
want: "",
},
{
// Filtered value
logger: NewFilter(With(NewStdLogger(buf), "caller", "caller"), FilterFunc(testFilterFuncWithLoggerPrefix)),
want: "INFO caller=caller msg=msg filtered=***\n",
},
{
// NO prefix
logger: NewFilter(With(NewStdLogger(buf)), FilterFunc(testFilterFuncWithLoggerPrefix)),
want: "INFO msg=msg filtered=***\n",
},
}
for _, tt := range tests {
err := tt.logger.Log(LevelInfo, "msg", "msg", "filtered", "true")
if err != nil {
t.Fatal("err should be nil")
}
got := buf.String()
if got != tt.want {
t.Fatalf("filter should catch prefix, want %s, got %s.", tt.want, got)
}
buf.Reset()
}
}
func testFilterFuncWithLoggerPrefix(level Level, keyvals ...interface{}) bool {
if level == LevelWarn {
return true
}
for i := 0; i < len(keyvals); i += 2 {
if keyvals[i] == "prefix" {
return true
}
if keyvals[i] == "filtered" {
keyvals[i+1] = fuzzyStr
}
}
return false
}