From 94ab6f3d8ec0bed7377fc199e1482faccb9495a2 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Fri, 8 Apr 2022 15:35:29 +0100 Subject: [PATCH] feat: add debug-level request logging (#923) This commit adds a small middleware to coderd that logs all requests at DEBUG level. --- coderd/coderd.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/coderd/coderd.go b/coderd/coderd.go index fc3ac5a9f3..b82f2eef9b 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -1,6 +1,8 @@ package coderd import ( + "context" + "fmt" "net/http" "net/url" "sync" @@ -56,6 +58,7 @@ func New(options *Options) (http.Handler, func()) { chitrace.Middleware(), // Specific routes can specify smaller limits. httpmw.RateLimitPerMinute(512), + debugLogRequest(api.Logger), ) r.Get("/", func(w http.ResponseWriter, r *http.Request) { httpapi.Write(w, http.StatusOK, httpapi.Response{ @@ -231,3 +234,12 @@ type api struct { websocketWaitMutex sync.Mutex websocketWaitGroup sync.WaitGroup } + +func debugLogRequest(log slog.Logger) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + log.Debug(context.Background(), fmt.Sprintf("%s %s", r.Method, r.URL.Path)) + next.ServeHTTP(rw, r) + }) + } +}