|
|
|
@ -3,10 +3,10 @@ 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 |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|