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

Function UserAgentHandler

hlog/hlog.go:125–137  ·  view source on GitHub ↗

UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.

(fieldKey string)

Source from the content-addressed store, hash-verified

123// UserAgentHandler adds the request's user-agent as a field to the context's logger
124// using fieldKey as field key.
125func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler {
126 return func(next http.Handler) http.Handler {
127 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
128 if ua := r.Header.Get("User-Agent"); ua != "" {
129 log := zerolog.Ctx(r.Context())
130 log.UpdateContext(func(c zerolog.Context) zerolog.Context {
131 return c.Str(fieldKey, ua)
132 })
133 }
134 next.ServeHTTP(w, r)
135 })
136 }
137}
138
139// RefererHandler adds the request's referer as a field to the context's logger
140// using fieldKey as field key.

Callers 2

Example_handlerFunction · 0.92
TestUserAgentHandlerFunction · 0.85

Calls 3

CtxFunction · 0.92
UpdateContextMethod · 0.80
StrMethod · 0.45

Tested by 2

Example_handlerFunction · 0.74
TestUserAgentHandlerFunction · 0.68