mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2025-12-30 09:38:26 -05:00
27 lines
790 B
Go
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))
|
|
})
|
|
}
|
|
}
|