update log filter

pull/1053/head
包子 3 years ago
parent 58c99b2768
commit ecb0030d48
  1. 19
      log/filter.go
  2. 9
      log/filter_test.go

@ -72,23 +72,24 @@ func (h *Filter) filter(level Level, keyvals []interface{}) bool {
if len(keyvals)%2 == 0 { if len(keyvals)%2 == 0 {
for i := 0; i < len(keyvals); i += 2 { for i := 0; i < len(keyvals); i += 2 {
if h.filterKey != nil { if h.filterKey != nil {
if _, ok := h.filterKey[keyvals[i].(string)]; ok { if v, ok := keyvals[i].(string); ok {
keyvals[i+1] = "***" if _, ok := h.filterKey[v]; ok {
keyvals[i+1] = "***"
}
} }
} }
if h.filterValue != nil { if h.filterValue != nil {
if _, ok := h.filterValue[keyvals[i].(string)]; ok { if v, ok := keyvals[i+1].(string); ok {
keyvals[i] = "***" if _, ok := h.filterValue[v]; ok {
} keyvals[i+1] = "***"
if _, ok := h.filterValue[keyvals[i+1].(string)]; ok { }
keyvals[i+1] = "***"
} }
} }
} }
} else { } else {
for i := 0; i < len(keyvals); i++ { for i := 0; i < len(keyvals); i++ {
if h.filterValue != nil { if v, ok := keyvals[i].(string); ok {
if _, ok := h.filterValue[keyvals[i].(string)]; ok { if _, ok := h.filterValue[v]; ok {
keyvals[i] = "***" keyvals[i] = "***"
} }
} }

@ -1,6 +1,7 @@
package log package log
import ( import (
"fmt"
"testing" "testing"
) )
@ -15,7 +16,7 @@ func TestFilterAll(t *testing.T) {
log.Log(LevelDebug, "msg", "test debug") log.Log(LevelDebug, "msg", "test debug")
log.Info("hello") log.Info("hello")
log.Infow("password", "123456") log.Infow("password", "123456")
log.Infow("username", "test debug") log.Infow("username", "kratos")
log.Warn("warn log") log.Warn("warn log")
} }
func TestFilterLevel(t *testing.T) { func TestFilterLevel(t *testing.T) {
@ -58,3 +59,9 @@ func testFilterFunc(level Level, keyvals ...interface{}) bool {
} }
return false return false
} }
func TestDY(t *testing.T) {
kvs := []interface{}{}
//s := kvs[0].(string)
fmt.Println(fmt.Sprint(kvs[0]))
}
Loading…
Cancel
Save