clean log filter

pull/1053/head
包子 4 years ago
parent ecb0030d48
commit 38673521e4
  1. 46
      log/filter.go
  2. 27
      log/filter_test.go
  3. 4
      log/value.go

@ -2,11 +2,11 @@ package log
// Filter is a logger filter.
type Filter struct {
logger Logger
filterLevel Level
filterKey map[string]struct{}
filterValue map[string]struct{}
filterFunc func(level Level, keyvals ...interface{}) bool
logger Logger
level Level
key map[string]struct{}
value map[string]struct{}
filterHook func(level Level, keyvals ...interface{}) bool
}
// FilterOption is filter option.
@ -15,36 +15,36 @@ type FilterOption func(*Filter)
// FilterLevel with filter level
func FilterLevel(level Level) FilterOption {
return func(opts *Filter) {
opts.filterLevel = level
opts.level = level
}
}
// FilterKey with filter key
func FilterKey(key ...string) FilterOption {
// FilterKeys with filter key
func FilterKeys(key ...string) FilterOption {
return func(opts *Filter) {
filterKey := make(map[string]struct{})
for _, v := range key {
filterKey[v] = struct{}{}
}
opts.filterKey = filterKey
opts.key = filterKey
}
}
// FilterValue with filter value
func FilterValue(value ...string) FilterOption {
// FilterValues with filter value
func FilterValues(value ...string) FilterOption {
return func(opts *Filter) {
filterValue := make(map[string]struct{})
for _, v := range value {
filterValue[v] = struct{}{}
}
opts.filterValue = filterValue
opts.value = filterValue
}
}
// FilterFunc with filter func
func FilterFunc(f func(level Level, keyvals ...interface{}) bool) FilterOption {
// FilterHook with filter func
func FilterHook(f func(level Level, keyvals ...interface{}) bool) FilterOption {
return func(opts *Filter) {
opts.filterFunc = f
opts.filterHook = f
}
}
@ -66,21 +66,21 @@ func (h *Filter) Log(level Level, keyvals ...interface{}) error {
}
func (h *Filter) filter(level Level, keyvals []interface{}) bool {
if h.filterLevel > level {
if h.level > level {
return true
}
if len(keyvals)%2 == 0 {
for i := 0; i < len(keyvals); i += 2 {
if h.filterKey != nil {
if h.key != nil {
if v, ok := keyvals[i].(string); ok {
if _, ok := h.filterKey[v]; ok {
if _, ok := h.key[v]; ok {
keyvals[i+1] = "***"
}
}
}
if h.filterValue != nil {
if h.value != nil {
if v, ok := keyvals[i+1].(string); ok {
if _, ok := h.filterValue[v]; ok {
if _, ok := h.value[v]; ok {
keyvals[i+1] = "***"
}
}
@ -89,14 +89,14 @@ func (h *Filter) filter(level Level, keyvals []interface{}) bool {
} else {
for i := 0; i < len(keyvals); i++ {
if v, ok := keyvals[i].(string); ok {
if _, ok := h.filterValue[v]; ok {
if _, ok := h.value[v]; ok {
keyvals[i] = "***"
}
}
}
}
if h.filterFunc != nil {
return h.filterFunc(level, keyvals...)
if h.filterHook != nil {
return h.filterHook(level, keyvals...)
}
return false
}

@ -1,7 +1,6 @@
package log
import (
"fmt"
"testing"
)
@ -9,9 +8,9 @@ func TestFilterAll(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger,
FilterLevel(LevelDebug),
FilterKey("username"),
FilterValue("hello"),
FilterFunc(testFilterFunc),
FilterKeys("username"),
FilterValues("hello"),
FilterHook(testFilterFunc),
))
log.Log(LevelDebug, "msg", "test debug")
log.Info("hello")
@ -29,21 +28,29 @@ func TestFilterLevel(t *testing.T) {
log.Warn("warn log")
}
func TestFilterCaller(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewFilter(NewFilter(logger))
log.Log(LevelDebug, "msg1", "te1st debug")
logHelper := NewHelper(NewFilter(NewFilter(logger)))
logHelper.Log(LevelDebug, "msg1", "te1st debug")
}
func TestFilterKey(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger, FilterKey("password")))
log := NewHelper(NewFilter(logger, FilterKeys("password")))
log.Debugw("password", "123456")
}
func TestFilterValue(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger, FilterValue("debug")))
log := NewHelper(NewFilter(logger, FilterValues("debug")))
log.Debugf("test %s", "debug")
}
func TestFilterFunc(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewHelper(NewFilter(logger, FilterFunc(testFilterFunc)))
log := NewHelper(NewFilter(logger, FilterHook(testFilterFunc)))
log.Debug("debug level")
log.Infow("password", "123456")
}
@ -58,10 +65,4 @@ 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]))
}

@ -36,9 +36,9 @@ func Caller(depth int) Valuer {
h := strings.LastIndex(file, "/log/helper.go")
f := strings.LastIndex(file, "/log/filter.go")
if f > 0 {
_, file, line, _ = runtime.Caller(depth + 1)
_, file, line, _ = runtime.Caller(depth + 2)
if strings.LastIndex(file, "/log/helper.go") > 0 {
_, file, line, _ = runtime.Caller(depth + 2)
_, file, line, _ = runtime.Caller(depth + 3)
}
}
if h > 0 {

Loading…
Cancel
Save