diff --git a/logger/logger_test.go b/logger/logger_test.go index db2be511..a893cf77 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -8,7 +8,8 @@ import ( func TestLogger(t *testing.T) { ctx := context.TODO() - l := NewLogger(WithLevel(TraceLevel)) + buf := bytes.NewBuffer(nil) + l := NewLogger(WithLevel(TraceLevel), WithOutput(buf)) if err := l.Init(); err != nil { t.Fatal(err) } @@ -16,6 +17,18 @@ func TestLogger(t *testing.T) { l.Warn(ctx, "warn_msg1") l.Fields(map[string]interface{}{"error": "test"}).Info(ctx, "error message") l.Warn(ctx, "first", " ", "second") + if !bytes.Contains(buf.Bytes(), []byte(`"level":"trace","msg":"trace_msg1"`)) { + t.Fatalf("logger error, buf %s", buf.Bytes()) + } + if !bytes.Contains(buf.Bytes(), []byte(`"warn","msg":"warn_msg1"`)) { + t.Fatalf("logger error, buf %s", buf.Bytes()) + } + if !bytes.Contains(buf.Bytes(), []byte(`"error":"test","level":"info","msg":"error message"`)) { + t.Fatalf("logger error, buf %s", buf.Bytes()) + } + if !bytes.Contains(buf.Bytes(), []byte(`"level":"warn","msg":"first second"`)) { + t.Fatalf("logger error, buf %s", buf.Bytes()) + } } func TestLoggerWrapper(t *testing.T) { @@ -35,3 +48,21 @@ func TestLoggerWrapper(t *testing.T) { t.Fatalf("omit not works, struct: %v, output: %s", s, buf.Bytes()) } } + +func TestOmitLoggerWrapper(t *testing.T) { + ctx := context.TODO() + buf := bytes.NewBuffer(nil) + l := NewOmitLogger(NewLogger(WithLevel(TraceLevel), WithOutput(buf))) + if err := l.Init(); err != nil { + t.Fatal(err) + } + type secret struct { + Name string + Passw string `logger:"omit"` + } + s := &secret{Name: "name", Passw: "secret"} + l.Errorf(ctx, "test %#+v", s) + if !bytes.Contains(buf.Bytes(), []byte(`logger.secret{Name:\"name\", Passw:\"\"}"`)) { + t.Fatalf("omit not works, struct: %v, output: %s", s, buf.Bytes()) + } +} diff --git a/logger/wrapper.go b/logger/wrapper.go index 1021ad4b..6f13b651 100644 --- a/logger/wrapper.go +++ b/logger/wrapper.go @@ -16,10 +16,92 @@ type LogfFunc func(ctx context.Context, level Level, msg string, args ...interfa type Wrapper interface { // Log logs message with needed level Log(LogFunc) LogFunc - // Log(ctx context.Context, level Level, args ...interface{}) // Logf logs message with needed level Logf(LogfFunc) LogfFunc - //Logf(ctx context.Context, level Level, msg string, args ...interface{}) +} + +type OmitLogger struct { + l Logger +} + +func NewOmitLogger(l Logger) Logger { + return &OmitLogger{l: l} +} + +func (w *OmitLogger) Init(opts ...Option) error { + return w.l.Init(append(opts, WrapLogger(NewOmitWrapper()))...) +} + +func (w *OmitLogger) V(level Level) bool { + return w.l.V(level) +} + +func (w *OmitLogger) Options() Options { + return w.l.Options() +} + +func (w *OmitLogger) Fields(fields map[string]interface{}) Logger { + return w.l.Fields(fields) +} + +func (w *OmitLogger) Info(ctx context.Context, args ...interface{}) { + w.l.Info(ctx, args...) +} + +func (w *OmitLogger) Trace(ctx context.Context, args ...interface{}) { + w.l.Trace(ctx, args...) +} + +func (w *OmitLogger) Debug(ctx context.Context, args ...interface{}) { + w.l.Debug(ctx, args...) +} + +func (w *OmitLogger) Warn(ctx context.Context, args ...interface{}) { + w.l.Warn(ctx, args...) +} + +func (w *OmitLogger) Error(ctx context.Context, args ...interface{}) { + w.l.Error(ctx, args...) +} + +func (w *OmitLogger) Fatal(ctx context.Context, args ...interface{}) { + w.l.Fatal(ctx, args...) +} + +func (w *OmitLogger) Infof(ctx context.Context, msg string, args ...interface{}) { + w.l.Infof(ctx, msg, args...) +} + +func (w *OmitLogger) Tracef(ctx context.Context, msg string, args ...interface{}) { + w.l.Tracef(ctx, msg, args...) +} + +func (w *OmitLogger) Debugf(ctx context.Context, msg string, args ...interface{}) { + w.l.Debugf(ctx, msg, args...) +} + +func (w *OmitLogger) Warnf(ctx context.Context, msg string, args ...interface{}) { + w.l.Warnf(ctx, msg, args...) +} + +func (w *OmitLogger) Errorf(ctx context.Context, msg string, args ...interface{}) { + w.l.Errorf(ctx, msg, args...) +} + +func (w *OmitLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) { + w.l.Fatalf(ctx, msg, args...) +} + +func (w *OmitLogger) Log(ctx context.Context, level Level, args ...interface{}) { + w.l.Log(ctx, level, args...) +} + +func (w *OmitLogger) Logf(ctx context.Context, level Level, msg string, args ...interface{}) { + w.l.Logf(ctx, level, msg, args...) +} + +func (w *OmitLogger) String() string { + return w.l.String() } type OmitWrapper struct{}