1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-03-19 21:28:00 +02:00

Add formatting helpers to loggers

This commit is contained in:
Aaron L 2018-02-16 11:56:47 -08:00
parent c89ca29827
commit 77987afb8a
2 changed files with 56 additions and 15 deletions

@ -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...))
}

@ -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)
}
}