diff --git a/CHANGELOG.md b/CHANGELOG.md index 185378a..46202f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - Updated spec file and rpkg version macro to be able to choose when the 'v' is included in the version. [#163](https://github.com/xmidt-org/scytale/pull/163) +- Reconfigured the Bascule Logger settings so that the logger isn't overwritten [#166](https://github.com/xmidt-org/scytale/pull/166) ## [v0.6.1] - Fixed url parsing bug where we were leaving a '/'. [#161](https://github.com/xmidt-org/scytale/pull/161) diff --git a/basculeLogging.go b/basculeLogging.go index a7866f4..d2f1051 100644 --- a/basculeLogging.go +++ b/basculeLogging.go @@ -10,6 +10,11 @@ import ( "github.com/xmidt-org/webpa-common/v2/logging" ) +// LoggerFunc is a strategy for adding key/value pairs (possibly) based on an HTTP request. +// Functions of this type must append key/value pairs to the supplied slice and then return +// the new slice. +type LoggerFunc func([]interface{}, *http.Request) []interface{} + func sanitizeHeaders(headers http.Header) (filtered http.Header) { filtered = headers.Clone() if authHeader := filtered.Get("Authorization"); authHeader != "" { @@ -22,11 +27,21 @@ func sanitizeHeaders(headers http.Header) (filtered http.Header) { return } -func setLogger(logger log.Logger) func(delegate http.Handler) http.Handler { +func setLogger(logger log.Logger, lf ...LoggerFunc) func(delegate http.Handler) http.Handler { + + if logger == nil { + panic("The base Logger cannot be nil") + } + return func(delegate http.Handler) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method} + for _, f := range lf { + if f != nil { + kvs = f(kvs, r) + } + } kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs) ctx := r.WithContext(logging.WithLogger(r.Context(), log.With(logger, kvs...))) delegate.ServeHTTP(w, ctx) diff --git a/primaryHandler.go b/primaryHandler.go index c67546a..e51da30 100644 --- a/primaryHandler.go +++ b/primaryHandler.go @@ -44,7 +44,6 @@ import ( "github.com/xmidt-org/webpa-common/v2/basculechecks" "github.com/xmidt-org/webpa-common/v2/basculemetrics" "github.com/xmidt-org/webpa-common/v2/logging" - "github.com/xmidt-org/webpa-common/v2/logging/logginghttp" "github.com/xmidt-org/webpa-common/v2/service" "github.com/xmidt-org/webpa-common/v2/service/monitor" "github.com/xmidt-org/webpa-common/v2/xhttp" @@ -158,11 +157,25 @@ func authChain(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (a basculehttp.WithEErrorResponseFunc(listener.OnErrorResponse), ) - constructors := []alice.Constructor{setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)} + constructors := []alice.Constructor{setLogger(logger, extractIntendedDestination), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)} return alice.New(constructors...), nil } +// custom logger func that extracts the intended destination of requests +func extractIntendedDestination(kv []interface{}, request *http.Request) []interface{} { + if deviceName := request.Header.Get("X-Webpa-Device-Name"); len(deviceName) > 0 { + return append(kv, "X-Webpa-Device-Name", deviceName) + } + + if variables := mux.Vars(request); len(variables) > 0 { + if deviceID := variables["deviceID"]; len(deviceID) > 0 { + return append(kv, "deviceID", deviceID) + } + } + return kv +} + // createEndpoints examines the configuration and produces an appropriate fanout.Endpoints, either using the configured // endpoints or service discovery. func createEndpoints(logger log.Logger, cfg fanout.Configuration, registry xmetrics.Registry, e service.Environment) (fanout.Endpoints, error) { @@ -298,27 +311,7 @@ func NewPrimaryHandler(logger log.Logger, v *viper.Viper, registry xmetrics.Regi xhttp.WriteError(response, http.StatusBadRequest, "Invalid endpoint") }) - fanoutChain := fanout.NewChain( - cfg, - logginghttp.SetLogger( - logger, - logginghttp.RequestInfo, - - // custom logger func that extracts the intended destination of requests - func(kv []interface{}, request *http.Request) []interface{} { - if deviceName := request.Header.Get("X-Webpa-Device-Name"); len(deviceName) > 0 { - return append(kv, "X-Webpa-Device-Name", deviceName) - } - - if variables := mux.Vars(request); len(variables) > 0 { - if deviceID := variables["deviceID"]; len(deviceID) > 0 { - return append(kv, "deviceID", deviceID) - } - } - return kv - }, candlelight.InjectTraceInfoInLogger(), - ), - ) + fanoutChain := fanout.NewChain(cfg) HTTPFanoutHandler := fanoutChain.Then( fanout.New(