fix(filter): filter out keyvals if keyFunc is provided (#2484)

pull/2492/head
Miguel Martínez Triviño 2 years ago committed by GitHub
parent 511f1917ce
commit 0f38511ea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      log/filter.go
  2. 13
      log/filter_test.go

@ -64,20 +64,18 @@ func (f *Filter) Log(level Level, keyvals ...interface{}) error {
if level < f.level { if level < f.level {
return nil return nil
} }
// fkv is used to provide a slice to contains both logger.prefix and keyvals for filter // prefixkv contains the slice of arguments defined as prefixes during the log initialization
var fkv []interface{} var prefixkv []interface{}
l, ok := f.logger.(*logger) l, ok := f.logger.(*logger)
if ok && len(l.prefix) > 0 { if ok && len(l.prefix) > 0 {
fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals)) prefixkv = make([]interface{}, 0, len(l.prefix))
fkv = append(fkv, l.prefix...) prefixkv = append(prefixkv, l.prefix...)
fkv = append(fkv, keyvals...)
} }
if !ok {
fkv = keyvals if f.filter != nil && (f.filter(level, prefixkv...) || f.filter(level, keyvals...)) {
}
if f.filter != nil && f.filter(level, fkv...) {
return nil return nil
} }
if len(f.key) > 0 || len(f.value) > 0 { if len(f.key) > 0 || len(f.value) > 0 {
for i := 0; i < len(keyvals); i += 2 { for i := 0; i < len(keyvals); i += 2 {
v := i + 1 v := i + 1

@ -102,13 +102,19 @@ func TestFilterFuncWitchLoggerPrefix(t *testing.T) {
want: "", want: "",
}, },
{ {
// Filtered value
logger: NewFilter(With(NewStdLogger(buf), "caller", "caller"), FilterFunc(testFilterFuncWithLoggerPrefix)), logger: NewFilter(With(NewStdLogger(buf), "caller", "caller"), FilterFunc(testFilterFuncWithLoggerPrefix)),
want: "INFO caller=caller msg=msg\n", 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 { for _, tt := range tests {
err := tt.logger.Log(LevelInfo, "msg", "msg") err := tt.logger.Log(LevelInfo, "msg", "msg", "filtered", "true")
if err != nil { if err != nil {
t.Fatal("err should be nil") t.Fatal("err should be nil")
} }
@ -128,6 +134,9 @@ func testFilterFuncWithLoggerPrefix(level Level, keyvals ...interface{}) bool {
if keyvals[i] == "prefix" { if keyvals[i] == "prefix" {
return true return true
} }
if keyvals[i] == "filtered" {
keyvals[i+1] = fuzzyStr
}
} }
return false return false
} }

Loading…
Cancel
Save