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

Function NewHandler

hlog/hlog.go:24–34  ·  view source on GitHub ↗

NewHandler injects log into requests context.

(log zerolog.Logger)

Source from the content-addressed store, hash-verified

22
23// NewHandler injects log into requests context.
24func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
25 return func(next http.Handler) http.Handler {
26 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27 // Create a copy of the logger (including internal context slice)
28 // to prevent data race when using UpdateContext.
29 l := log.With().Logger()
30 r = r.WithContext(l.WithContext(r.Context()))
31 next.ServeHTTP(w, r)
32 })
33 }
34}
35
36// URLHandler adds the requested URL as a field to the context's logger
37// using fieldKey as field key.

Callers 15

Example_handlerFunction · 0.92
TestNewHandlerFunction · 0.85
TestURLHandlerFunction · 0.85
TestMethodHandlerFunction · 0.85
TestRequestHandlerFunction · 0.85
TestRemoteAddrHandlerFunction · 0.85
TestRemoteIPHandlerFunction · 0.85
TestRemoteIPHandlerIPv6Function · 0.85
TestUserAgentHandlerFunction · 0.85
TestRefererHandlerFunction · 0.85
TestRequestIDHandlerFunction · 0.85

Calls 3

WithFunction · 0.92
LoggerMethod · 0.80
WithContextMethod · 0.80

Tested by 15

Example_handlerFunction · 0.74
TestNewHandlerFunction · 0.68
TestURLHandlerFunction · 0.68
TestMethodHandlerFunction · 0.68
TestRequestHandlerFunction · 0.68
TestRemoteAddrHandlerFunction · 0.68
TestRemoteIPHandlerFunction · 0.68
TestRemoteIPHandlerIPv6Function · 0.68
TestUserAgentHandlerFunction · 0.68
TestRefererHandlerFunction · 0.68
TestRequestIDHandlerFunction · 0.68