2021-10-02 19:55:07 +03:00
|
|
|
package logger // import "go.unistack.org/micro/v3/logger/wrapper"
|
2021-07-05 22:32:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2021-07-05 22:32:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogFunc function used for Log method
|
|
|
|
type LogFunc func(ctx context.Context, level Level, args ...interface{})
|
|
|
|
|
|
|
|
// LogfFunc function used for Logf method
|
|
|
|
type LogfFunc func(ctx context.Context, level Level, msg string, args ...interface{})
|
|
|
|
|
|
|
|
type Wrapper interface {
|
|
|
|
// Log logs message with needed level
|
|
|
|
Log(LogFunc) LogFunc
|
|
|
|
// Logf logs message with needed level
|
|
|
|
Logf(LogfFunc) LogfFunc
|
2021-07-05 23:04:20 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
var _ Logger = &omitLogger{}
|
2021-07-14 17:12:09 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
type omitLogger struct {
|
2021-07-05 23:04:20 +03:00
|
|
|
l Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOmitLogger(l Logger) Logger {
|
2022-01-10 16:47:37 +03:00
|
|
|
return &omitLogger{l: l}
|
2021-07-05 23:04:20 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Init(opts ...Option) error {
|
2021-07-05 23:04:20 +03:00
|
|
|
return w.l.Init(append(opts, WrapLogger(NewOmitWrapper()))...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) V(level Level) bool {
|
2021-07-05 23:04:20 +03:00
|
|
|
return w.l.V(level)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Level(level Level) {
|
2021-08-30 16:19:35 +03:00
|
|
|
w.l.Level(level)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Clone(opts ...Option) Logger {
|
2021-08-30 16:19:35 +03:00
|
|
|
return w.l.Clone(opts...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Options() Options {
|
2021-07-05 23:04:20 +03:00
|
|
|
return w.l.Options()
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Fields(fields ...interface{}) Logger {
|
2021-08-06 02:15:57 +03:00
|
|
|
return w.l.Fields(fields...)
|
2021-07-05 23:04:20 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Info(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Info(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Trace(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Trace(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Debug(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Debug(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Warn(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Warn(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Error(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Error(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Fatal(ctx context.Context, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Fatal(ctx, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Infof(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Tracef(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Debugf(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Warnf(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Errorf(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Fatalf(ctx, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Log(ctx context.Context, level Level, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Log(ctx, level, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{}) {
|
2021-07-05 23:04:20 +03:00
|
|
|
w.l.Logf(ctx, level, msg, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitLogger) String() string {
|
2021-07-05 23:04:20 +03:00
|
|
|
return w.l.String()
|
2021-07-05 22:32:47 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
type omitWrapper struct{}
|
2021-07-05 22:32:47 +03:00
|
|
|
|
|
|
|
func NewOmitWrapper() Wrapper {
|
2022-01-10 16:47:37 +03:00
|
|
|
return &omitWrapper{}
|
2021-07-05 22:32:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getArgs(args []interface{}) []interface{} {
|
|
|
|
nargs := make([]interface{}, 0, len(args))
|
|
|
|
var err error
|
|
|
|
for _, arg := range args {
|
|
|
|
val := reflect.ValueOf(arg)
|
2021-09-28 23:43:43 +03:00
|
|
|
if val.Kind() == reflect.Ptr {
|
2021-07-05 22:32:47 +03:00
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
narg := arg
|
2021-09-28 23:43:43 +03:00
|
|
|
if val.Kind() != reflect.Struct {
|
|
|
|
nargs = append(nargs, narg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if narg, err = rutil.Zero(arg); err != nil {
|
|
|
|
nargs = append(nargs, narg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rutil.CopyDefaults(narg, arg)
|
|
|
|
if flds, ferr := rutil.StructFields(narg); ferr == nil {
|
|
|
|
for _, fld := range flds {
|
|
|
|
if tv, ok := fld.Field.Tag.Lookup("logger"); ok && tv == "omit" {
|
|
|
|
fld.Value.Set(reflect.Zero(fld.Value.Type()))
|
2021-07-05 22:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 23:43:43 +03:00
|
|
|
|
2021-07-05 22:32:47 +03:00
|
|
|
nargs = append(nargs, narg)
|
|
|
|
}
|
|
|
|
return nargs
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitWrapper) Log(fn LogFunc) LogFunc {
|
2021-07-05 22:32:47 +03:00
|
|
|
return func(ctx context.Context, level Level, args ...interface{}) {
|
|
|
|
fn(ctx, level, getArgs(args)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (w *omitWrapper) Logf(fn LogfFunc) LogfFunc {
|
2021-07-05 22:32:47 +03:00
|
|
|
return func(ctx context.Context, level Level, msg string, args ...interface{}) {
|
|
|
|
fn(ctx, level, msg, getArgs(args)...)
|
|
|
|
}
|
|
|
|
}
|