diff --git a/log/filter.go b/log/filter.go index c6ef7f605..a5c818f17 100644 --- a/log/filter.go +++ b/log/filter.go @@ -72,23 +72,24 @@ func (h *Filter) filter(level Level, keyvals []interface{}) bool { if len(keyvals)%2 == 0 { for i := 0; i < len(keyvals); i += 2 { if h.filterKey != nil { - if _, ok := h.filterKey[keyvals[i].(string)]; ok { - keyvals[i+1] = "***" + if v, ok := keyvals[i].(string); ok { + if _, ok := h.filterKey[v]; ok { + keyvals[i+1] = "***" + } } } if h.filterValue != nil { - if _, ok := h.filterValue[keyvals[i].(string)]; ok { - keyvals[i] = "***" - } - if _, ok := h.filterValue[keyvals[i+1].(string)]; ok { - keyvals[i+1] = "***" + if v, ok := keyvals[i+1].(string); ok { + if _, ok := h.filterValue[v]; ok { + keyvals[i+1] = "***" + } } } } } else { for i := 0; i < len(keyvals); i++ { - if h.filterValue != nil { - if _, ok := h.filterValue[keyvals[i].(string)]; ok { + if v, ok := keyvals[i].(string); ok { + if _, ok := h.filterValue[v]; ok { keyvals[i] = "***" } } diff --git a/log/filter_test.go b/log/filter_test.go index b5b2be9fa..11ce79f46 100644 --- a/log/filter_test.go +++ b/log/filter_test.go @@ -1,6 +1,7 @@ package log import ( + "fmt" "testing" ) @@ -15,7 +16,7 @@ func TestFilterAll(t *testing.T) { log.Log(LevelDebug, "msg", "test debug") log.Info("hello") log.Infow("password", "123456") - log.Infow("username", "test debug") + log.Infow("username", "kratos") log.Warn("warn log") } func TestFilterLevel(t *testing.T) { @@ -58,3 +59,9 @@ func testFilterFunc(level Level, keyvals ...interface{}) bool { } return false } + +func TestDY(t *testing.T) { + kvs := []interface{}{} + //s := kvs[0].(string) + fmt.Println(fmt.Sprint(kvs[0])) +} \ No newline at end of file