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)
| 306 | // using fieldKey as field key. If trimPort is set to true, then port is |
| 307 | // removed from the host. |
| 308 | func 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 | } |