mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-07 23:02:12 +02:00
clean log filter and update value caller
This commit is contained in:
parent
582a24c0a6
commit
58c99b2768
106
log/filter.go
106
log/filter.go
@ -1,9 +1,5 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Filter is a logger filter.
|
||||
type Filter struct {
|
||||
logger Logger
|
||||
@ -62,107 +58,11 @@ func NewFilter(logger Logger, opts ...FilterOption) *Filter {
|
||||
}
|
||||
|
||||
// Log .
|
||||
func (h *Filter) Log(level Level, keyvals ...interface{}) {
|
||||
func (h *Filter) Log(level Level, keyvals ...interface{}) error {
|
||||
if f := h.filter(level, keyvals); f {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
h.logger.Log(level, keyvals...)
|
||||
}
|
||||
|
||||
// Debug logs a message at debug level.
|
||||
func (h *Filter) Debug(a ...interface{}) {
|
||||
if f := h.filter(LevelDebug, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelDebug, "msg", fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Debugf logs a message at debug level.
|
||||
func (h *Filter) Debugf(format string, a ...interface{}) {
|
||||
if f := h.filter(LevelDebug, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelDebug, "msg", fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Debugw logs a message at debug level.
|
||||
func (h *Filter) Debugw(keyvals ...interface{}) {
|
||||
if f := h.filter(LevelDebug, keyvals); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelDebug, keyvals...)
|
||||
}
|
||||
|
||||
// Info logs a message at info level.
|
||||
func (h *Filter) Info(a ...interface{}) {
|
||||
if f := h.filter(LevelInfo, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelInfo, "msg", fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Infof logs a message at info level.
|
||||
func (h *Filter) Infof(format string, a ...interface{}) {
|
||||
if f := h.filter(LevelInfo, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelInfo, "msg", fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Infow logs a message at info level.
|
||||
func (h *Filter) Infow(keyvals ...interface{}) {
|
||||
if f := h.filter(LevelInfo, keyvals); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelInfo, keyvals...)
|
||||
}
|
||||
|
||||
// Warn logs a message at warn level.
|
||||
func (h *Filter) Warn(a ...interface{}) {
|
||||
if f := h.filter(LevelWarn, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelWarn, "msg", fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Warnf logs a message at warnf level.
|
||||
func (h *Filter) Warnf(format string, a ...interface{}) {
|
||||
if f := h.filter(LevelWarn, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelWarn, "msg", fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Warnw logs a message at warnf level.
|
||||
func (h *Filter) Warnw(keyvals ...interface{}) {
|
||||
if f := h.filter(LevelWarn, keyvals); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelWarn, keyvals...)
|
||||
}
|
||||
|
||||
// Error logs a message at error level.
|
||||
func (h *Filter) Error(a ...interface{}) {
|
||||
if f := h.filter(LevelError, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelError, "msg", fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Errorf logs a message at error level.
|
||||
func (h *Filter) Errorf(format string, a ...interface{}) {
|
||||
if f := h.filter(LevelError, a); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelError, "msg", fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Errorw logs a message at error level.
|
||||
func (h *Filter) Errorw(keyvals ...interface{}) {
|
||||
if f := h.filter(LevelError, keyvals); f {
|
||||
return
|
||||
}
|
||||
h.logger.Log(LevelError, keyvals...)
|
||||
return h.logger.Log(level, keyvals...)
|
||||
}
|
||||
|
||||
func (h *Filter) filter(level Level, keyvals []interface{}) bool {
|
||||
|
@ -6,13 +6,12 @@ import (
|
||||
|
||||
func TestFilterAll(t *testing.T) {
|
||||
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
log := NewFilter(logger,
|
||||
log := NewHelper(NewFilter(logger,
|
||||
FilterLevel(LevelDebug),
|
||||
FilterKey("username"),
|
||||
FilterValue("hello"),
|
||||
FilterFunc(testFilterFunc),
|
||||
)
|
||||
|
||||
))
|
||||
log.Log(LevelDebug, "msg", "test debug")
|
||||
log.Info("hello")
|
||||
log.Infow("password", "123456")
|
||||
@ -21,7 +20,7 @@ func TestFilterAll(t *testing.T) {
|
||||
}
|
||||
func TestFilterLevel(t *testing.T) {
|
||||
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
log := NewFilter(logger, FilterLevel(LevelWarn))
|
||||
log := NewHelper(NewFilter(NewFilter(logger, FilterLevel(LevelWarn))))
|
||||
log.Log(LevelDebug, "msg1", "te1st debug")
|
||||
log.Debug("test debug")
|
||||
log.Debugf("test %s", "debug")
|
||||
@ -31,19 +30,19 @@ func TestFilterLevel(t *testing.T) {
|
||||
|
||||
func TestFilterKey(t *testing.T) {
|
||||
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
log := NewFilter(logger, FilterKey("password"))
|
||||
log := NewHelper(NewFilter(logger, FilterKey("password")))
|
||||
log.Debugw("password", "123456")
|
||||
}
|
||||
|
||||
func TestFilterValue(t *testing.T) {
|
||||
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
log := NewFilter(logger, FilterValue("debug"))
|
||||
log := NewHelper(NewFilter(logger, FilterValue("debug")))
|
||||
log.Debugf("test %s", "debug")
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
|
||||
log := NewFilter(logger, FilterFunc(testFilterFunc))
|
||||
log := NewHelper(NewFilter(logger, FilterFunc(testFilterFunc)))
|
||||
log.Debug("debug level")
|
||||
log.Infow("password", "123456")
|
||||
}
|
||||
|
@ -35,7 +35,13 @@ func Caller(depth int) Valuer {
|
||||
_, file, line, _ := runtime.Caller(depth)
|
||||
h := strings.LastIndex(file, "/log/helper.go")
|
||||
f := strings.LastIndex(file, "/log/filter.go")
|
||||
if h > 0 || f > 0 {
|
||||
if f > 0 {
|
||||
_, file, line, _ = runtime.Caller(depth + 1)
|
||||
if strings.LastIndex(file, "/log/helper.go") > 0 {
|
||||
_, file, line, _ = runtime.Caller(depth + 2)
|
||||
}
|
||||
}
|
||||
if h > 0 {
|
||||
_, file, line, _ = runtime.Caller(depth + 1)
|
||||
}
|
||||
idx := strings.LastIndexByte(file, '/')
|
||||
|
Loading…
Reference in New Issue
Block a user