mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-22 03:38:41 +02:00
85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package log
|
|
|
|
// FilterOption is filter option.
|
|
type FilterOption func(*Filter)
|
|
|
|
// FilterLevel with filter level.
|
|
func FilterLevel(level Level) FilterOption {
|
|
return func(opts *Filter) {
|
|
opts.level = level
|
|
}
|
|
}
|
|
|
|
// FilterKey with filter key.
|
|
func FilterKey(key ...string) FilterOption {
|
|
return func(o *Filter) {
|
|
for _, v := range key {
|
|
o.key[v] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// FilterValue with filter value.
|
|
func FilterValue(value ...string) FilterOption {
|
|
return func(o *Filter) {
|
|
for _, v := range value {
|
|
o.value[v] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// FilterFunc with filter func.
|
|
func FilterFunc(f func(level Level, keyvals ...interface{}) bool) FilterOption {
|
|
return func(o *Filter) {
|
|
o.filter = f
|
|
}
|
|
}
|
|
|
|
// Filter is a logger filter.
|
|
type Filter struct {
|
|
logger Logger
|
|
level Level
|
|
key map[string]struct{}
|
|
value map[string]struct{}
|
|
filter func(level Level, keyvals ...interface{}) bool
|
|
}
|
|
|
|
// NewFilter new a logger filter.
|
|
func NewFilter(logger Logger, opts ...FilterOption) *Filter {
|
|
options := Filter{
|
|
logger: logger,
|
|
key: make(map[string]struct{}),
|
|
value: make(map[string]struct{}),
|
|
}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
return &options
|
|
}
|
|
|
|
// Log .
|
|
func (f *Filter) Log(level Level, keyvals ...interface{}) error {
|
|
if f.level > level {
|
|
return nil
|
|
}
|
|
if f.filter != nil && f.filter(level, keyvals...) {
|
|
return nil
|
|
}
|
|
for i := 0; i < len(keyvals); i += 2 {
|
|
if i >= len(keyvals) {
|
|
continue
|
|
}
|
|
if v, ok := keyvals[i].(string); ok {
|
|
if _, ok := f.key[v]; ok {
|
|
keyvals[i+1] = "***"
|
|
}
|
|
}
|
|
if v, ok := keyvals[i+1].(string); ok {
|
|
if _, ok := f.value[v]; ok {
|
|
keyvals[i+1] = "***"
|
|
}
|
|
}
|
|
}
|
|
return f.logger.Log(level, keyvals...)
|
|
}
|