Motivation #
Previously, cmd/goosefoot constructed a *slog.Logger and mutated the process-wide global default via slog.SetDefault(logger) because server.New and middleware.Logging had no explicit logger dependency:
middleware.Loggingreached intolog.FromContext(r.Context())on incoming requests, implicitly falling back toslog.Default()in production and requiring unit tests to pre-populatereq.Context()before the middleware ran.server.Newacceptedrunnerexplicitly but omitted*slog.Logger, leavinghttp.Server.ErrorLogbound to the globallog.Default()writer and causing server unit tests to leak request logs to globalos.Stderr.- Calling
run()incmd/goosefoottests overwrote process-globalslogandlogstate with test-scoped buffers.
Summary of Changes #
internal/middleware: ChangeLogging(base *slog.Logger) func(http.Handler) http.Handlerto accept its base logger explicitly at construction time (aligning withAuthandMaxBodySize), fail fast onnil, and attach the enriched request logger tor.Context()for downstream handlers.internal/server: Require a non-nil*slog.Loggerinserver.New(cfg, runner, logger), wiremiddleware.Logging(s.logger)on the router, and routehttp.Server.ErrorLogthroughslog.NewLogLogger(s.logger.Handler(), slog.LevelError).cmd/goosefoot: Passloggerdirectly toserver.Newand remove the globalslog.SetDefault(logger)mutation.- Tests: Update middleware and server tests to inject loggers through public constructors (
slog.DiscardHandlerby default innewTestServer), and add coverage for fail-fastnillogger checks and preservation ofslog.Default()acrossrun().