MCPcopy
hub / github.com/rs/zerolog / HostHandler

Function HostHandler

hlog/hlog.go:308–326  ·  view source on GitHub ↗

HostHandler adds the request's host as a field to the context's logger using fieldKey as field key. If trimPort is set to true, then port is removed from the host.

(fieldKey string, trimPort ...bool)

Source from the content-addressed store, hash-verified

306// using fieldKey as field key. If trimPort is set to true, then port is
307// removed from the host.
308func HostHandler(fieldKey string, trimPort ...bool) func(next http.Handler) http.Handler {
309 return func(next http.Handler) http.Handler {
310 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
311 var host string
312 if len(trimPort) > 0 && trimPort[0] {
313 host = getHost(r.Host)
314 } else {
315 host = r.Host
316 }
317 if host != "" {
318 log := zerolog.Ctx(r.Context())
319 log.UpdateContext(func(c zerolog.Context) zerolog.Context {
320 return c.Str(fieldKey, host)
321 })
322 }
323 next.ServeHTTP(w, r)
324 })
325 }
326}

Callers 2

TestHostHandlerFunction · 0.85

Calls 4

CtxFunction · 0.92
getHostFunction · 0.85
UpdateContextMethod · 0.80
StrMethod · 0.45

Tested by 2

TestHostHandlerFunction · 0.68