| 272 | } |
| 273 | |
| 274 | func ResponseHeaderHandler(fieldKey, headerName string) func(next http.Handler) http.Handler { |
| 275 | return func(next http.Handler) http.Handler { |
| 276 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 277 | defer func() { |
| 278 | value := w.Header().Get(headerName) |
| 279 | if value != "" { |
| 280 | log := zerolog.Ctx(r.Context()) |
| 281 | log.UpdateContext(func(c zerolog.Context) zerolog.Context { |
| 282 | return c.Str(fieldKey, value) |
| 283 | }) |
| 284 | } |
| 285 | }() |
| 286 | next.ServeHTTP(w, r) |
| 287 | }) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // AccessHandler returns a handler that call f after each request. |
| 292 | func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler { |