From 6e7f81d73bcb1d163f9345d68dc56911951c6e83 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Tue, 10 Sep 2019 17:55:13 -0400 Subject: [PATCH] leveled: fix default logger and add a test --- leveled/logger.go | 10 +++++----- leveled/logger_test.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 leveled/logger_test.go diff --git a/leveled/logger.go b/leveled/logger.go index bcb4024..474f66b 100644 --- a/leveled/logger.go +++ b/leveled/logger.go @@ -41,30 +41,30 @@ func Default(logger *alog.Logger) Logger { // Debug implements Logger.Debug func (d *defaultLogger) Debug(ctx context.Context, f string, v ...interface{}) { - alog.AddTags(ctx, "level", "debug") + ctx = alog.AddTags(ctx, "level", "debug") d.Logger.Output(ctx, 3, fmt.Sprintf(f, v...)) } // Info implements Logger.Info func (d *defaultLogger) Info(ctx context.Context, f string, v ...interface{}) { - alog.AddTags(ctx, "level", "info") + ctx = alog.AddTags(ctx, "level", "info") d.Logger.Output(ctx, 3, fmt.Sprintf(f, v...)) } // Warning implements Logger.Warning func (d *defaultLogger) Warning(ctx context.Context, f string, v ...interface{}) { - alog.AddTags(ctx, "level", "warning") + ctx = alog.AddTags(ctx, "level", "warning") d.Logger.Output(ctx, 3, fmt.Sprintf(f, v...)) } // Error implements Logger.Error func (d *defaultLogger) Error(ctx context.Context, f string, v ...interface{}) { - alog.AddTags(ctx, "level", "error") + ctx = alog.AddTags(ctx, "level", "error") d.Logger.Output(ctx, 3, fmt.Sprintf(f, v...)) } // Critical implements Logger.Critical func (d *defaultLogger) Critical(ctx context.Context, f string, v ...interface{}) { - alog.AddTags(ctx, "level", "critical") + ctx = alog.AddTags(ctx, "level", "critical") d.Logger.Output(ctx, 3, fmt.Sprintf(f, v...)) } diff --git a/leveled/logger_test.go b/leveled/logger_test.go new file mode 100644 index 0000000..0b697a7 --- /dev/null +++ b/leveled/logger_test.go @@ -0,0 +1,23 @@ +package leveled + +import ( + "bytes" + "context" + "testing" + + "github.com/vimeo/alog/v3" + "github.com/vimeo/alog/v3/emitter/textlog" +) + +func TestLogger(t *testing.T) { + b := &bytes.Buffer{} + l := Default(alog.New(alog.WithEmitter(textlog.Emitter(b)))) + + ctx := context.Background() + ctx = alog.AddTags(ctx, "key", "value") + l.Info(ctx, "") + const want = `[key=value level=info] ` + "\n" + if got := b.String(); got != want { + t.Errorf("got: %#q, want: %#q", got, want) + } +}