RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.
(fieldKey string)
| 78 | // RemoteAddrHandler adds the request's remote address as a field to the context's logger |
| 79 | // using fieldKey as field key. |
| 80 | func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler { |
| 81 | return func(next http.Handler) http.Handler { |
| 82 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 83 | if r.RemoteAddr != "" { |
| 84 | log := zerolog.Ctx(r.Context()) |
| 85 | log.UpdateContext(func(c zerolog.Context) zerolog.Context { |
| 86 | return c.Str(fieldKey, r.RemoteAddr) |
| 87 | }) |
| 88 | } |
| 89 | next.ServeHTTP(w, r) |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func getHost(hostPort string) string { |
| 95 | if hostPort == "" { |