feat: add debug-level request logging (#923)

This commit adds a small middleware to coderd that logs all requests at DEBUG level.
This commit is contained in:
Cian Johnston 2022-04-08 15:35:29 +01:00 committed by GitHub
parent 38f074254b
commit 94ab6f3d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

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