linstrom/server/middlewares/injectLogrus.go
2024-08-28 17:20:38 +02:00

17 lines
479 B
Go

package middlewares
import (
"net/http"
"github.com/rs/zerolog/log"
)
const CONTEXT_KEY_LOGRUS = ContextKey("logrus")
// Inject a logrus entry into the context that has the url path already set
func InjectLogrusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newContext := log.With().Str("url-path", r.URL.RawPath).Logger().WithContext(r.Context())
next.ServeHTTP(w, r.WithContext(newContext))
})
}