mirror of
https://github.com/volatiletech/authboss.git
synced 2025-01-24 05:17:10 +02:00
Work on logging and error handling some more
This commit is contained in:
parent
27010d9fe4
commit
cf05c8d36b
@ -85,6 +85,9 @@ type Config struct {
|
||||
// modules will register their routes with it.
|
||||
Router Router
|
||||
|
||||
// ErrorHandler wraps http requests with centralized error handling.
|
||||
ErrorHandler ErrorHandler
|
||||
|
||||
// Responder takes a generic response from a controller and prepares
|
||||
// the response, uses a renderer to create the body, and replies to the
|
||||
// http request.
|
||||
|
20
logger.go
20
logger.go
@ -2,6 +2,7 @@ package authboss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Logger is the basic logging structure that's required
|
||||
@ -12,7 +13,24 @@ type Logger interface {
|
||||
|
||||
// ContextLogger creates a logger from a request context
|
||||
type ContextLogger interface {
|
||||
FromContext(ctx context.Context) Logger
|
||||
FromContext(context.Context) Logger
|
||||
}
|
||||
|
||||
// RequestLogger creates a logger from a request
|
||||
type RequestLogger interface {
|
||||
FromRequest(*http.Request) Logger
|
||||
}
|
||||
|
||||
// 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 {
|
||||
logger := a.Config.Core.Logger
|
||||
if reqLogger, ok := logger.(RequestLogger); ok {
|
||||
return reqLogger.FromRequest(r)
|
||||
}
|
||||
|
||||
return a.Logger(r.Context())
|
||||
}
|
||||
|
||||
// Logger returns an appopriate logger for the context:
|
||||
|
@ -2,6 +2,8 @@ package authboss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -14,6 +16,7 @@ func (t testLogger) Info(string) {}
|
||||
func (t testLogger) Error(string) {}
|
||||
|
||||
func (t testLogger) FromContext(ctx context.Context) Logger { return testCtxLogger{} }
|
||||
func (t testLogger) FromRequest(r *http.Request) Logger { return testLogger{} }
|
||||
|
||||
func (t testCtxLogger) Info(string) {}
|
||||
func (t testCtxLogger) Error(string) {}
|
||||
@ -32,4 +35,8 @@ func TestLogger(t *testing.T) {
|
||||
if _, ok := ab.Logger(context.Background()).(testCtxLogger); !ok {
|
||||
t.Error("wanted ctx logger back")
|
||||
}
|
||||
|
||||
if _, ok := ab.RequestLogger(httptest.NewRequest("GET", "/", nil)).(testLogger); !ok {
|
||||
t.Error("wanted normal logger back")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user