linstrom/server/middlewares/injectLogrus.go

18 lines
479 B
Go
Raw Normal View History

2024-05-31 09:54:39 +00:00
package middlewares
import (
"net/http"
2024-08-28 15:20:38 +00:00
"github.com/rs/zerolog/log"
2024-05-31 09:54:39 +00:00
)
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) {
2024-08-28 15:20:38 +00:00
newContext := log.With().Str("url-path", r.URL.RawPath).Logger().WithContext(r.Context())
2024-05-31 09:54:39 +00:00
next.ServeHTTP(w, r.WithContext(newContext))
})
}