Files
opencloud/services/proxy/pkg/middleware/context_logger.go
Jörn Friedrich Dreyer b07b5a1149 use plain pkg module
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
2025-01-13 16:42:19 +01:00

27 lines
790 B
Go

package middleware
import (
"net/http"
"github.com/opencloud-eu/opencloud/pkg/log"
)
// ContextLogger is a middleware to use a logger associated with the request's
// context which includes general information of the request.
func ContextLogger(logger log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := logger.With().
Str("remoteAddr", r.RemoteAddr).
Str(log.RequestIDString, r.Header.Get("X-Request-ID")).
Str("proto", r.Proto).
Str("method", r.Method).
Str("path", r.URL.Path).
Str("query", r.URL.RawQuery).
Str("fragment", r.URL.Fragment).
Logger().WithContext(r.Context())
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}