From 77987afb8a283ebf56e7a291e10fefe8da29f0de Mon Sep 17 00:00:00 2001 From: Aaron L Date: Fri, 16 Feb 2018 11:56:47 -0800 Subject: [PATCH] Add formatting helpers to loggers --- logger.go | 31 ++++++++++++++++++++++++------- logger_test.go | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/logger.go b/logger.go index fcb2b3d..1eb7758 100644 --- a/logger.go +++ b/logger.go @@ -2,6 +2,7 @@ package authboss import ( "context" + "fmt" "net/http" ) @@ -24,13 +25,13 @@ type RequestLogger interface { // RequestLogger returns a request logger if possible, if not // it calls Logger which tries to do a ContextLogger, and if // that fails it will finally get a normal logger. -func (a *Authboss) RequestLogger(r *http.Request) Logger { +func (a *Authboss) RequestLogger(r *http.Request) FmtLogger { logger := a.Config.Core.Logger if reqLogger, ok := logger.(RequestLogger); ok { - return reqLogger.FromRequest(r) + return FmtLogger{reqLogger.FromRequest(r)} } - return a.Logger(r.Context()) + return FmtLogger{a.Logger(r.Context())} } // Logger returns an appopriate logger for the context: @@ -39,16 +40,32 @@ func (a *Authboss) RequestLogger(r *http.Request) Logger { // If context is not nil, then it will attempt to upgrade // the configured logger to a ContextLogger, and create // a context-specific logger for use. -func (a *Authboss) Logger(ctx context.Context) Logger { +func (a *Authboss) Logger(ctx context.Context) FmtLogger { logger := a.Config.Core.Logger if ctx == nil { - return logger + return FmtLogger{logger} } ctxLogger, ok := logger.(ContextLogger) if !ok { - return logger + return FmtLogger{logger} } - return ctxLogger.FromContext(ctx) + return FmtLogger{ctxLogger.FromContext(ctx)} +} + +// FmtLogger adds convenience functions on top of the logging +// methods for formatting. +type FmtLogger struct { + Logger +} + +// Errorf prints to Error() with fmt.Printf semantics +func (f FmtLogger) Errorf(format string, values ...interface{}) { + f.Logger.Error(fmt.Sprintf(format, values...)) +} + +// Infof prints to Info() with fmt.Printf semantics +func (f FmtLogger) Infof(format string, values ...interface{}) { + f.Logger.Info(fmt.Sprintf(format, values...)) } diff --git a/logger_test.go b/logger_test.go index 08f0c96..46955bd 100644 --- a/logger_test.go +++ b/logger_test.go @@ -8,15 +8,22 @@ import ( ) type ( - testLogger struct{} + testLogger struct { + info string + error string + } testCtxLogger struct{} ) -func (t testLogger) Info(string) {} -func (t testLogger) Error(string) {} +func (t *testLogger) Info(s string) { + t.info += s +} +func (t *testLogger) Error(s string) { + t.error += s +} func (t testLogger) FromContext(ctx context.Context) Logger { return testCtxLogger{} } -func (t testLogger) FromRequest(r *http.Request) Logger { return testLogger{} } +func (t testLogger) FromRequest(r *http.Request) Logger { return &testLogger{} } func (t testCtxLogger) Info(string) {} func (t testCtxLogger) Error(string) {} @@ -25,18 +32,35 @@ func TestLogger(t *testing.T) { t.Parallel() ab := New() - logger := testLogger{} + logger := &testLogger{} ab.Config.Core.Logger = logger - if logger != ab.Logger(nil).(testLogger) { + if logger != ab.Logger(nil).Logger.(*testLogger) { t.Error("wanted our logger back") } - if _, ok := ab.Logger(context.Background()).(testCtxLogger); !ok { + if _, ok := ab.Logger(context.Background()).Logger.(testCtxLogger); !ok { t.Error("wanted ctx logger back") } - if _, ok := ab.RequestLogger(httptest.NewRequest("GET", "/", nil)).(testLogger); !ok { + if _, ok := ab.RequestLogger(httptest.NewRequest("GET", "/", nil)).Logger.(*testLogger); !ok { t.Error("wanted normal logger back") } } + +func TestFmtLogger(t *testing.T) { + t.Parallel() + + logger := &testLogger{} + fmtlog := FmtLogger{logger} + + fmtlog.Errorf("%s %s", "ok", "go") + fmtlog.Infof("%s %s", "go", "ok") + + if logger.error != "ok go" { + t.Error("wrong output", logger.error) + } + if logger.info != "go ok" { + t.Error("wrong output", logger.info) + } +}