Documentation ¶
Overview ¶
Package hlog provides a set of http.Handler helpers for zerolog. ## COPY of zero log helpers to get access to context values ##
Index ¶
- func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler
- func ContextTransfer(parentCtx, out context.Context) context.Context
- func CustomHeaderHandler(fieldKey, header string) func(next http.Handler) http.Handler
- func FromRequest(r *http.Request) *zerolog.Logger
- func IDFromCtx(ctx context.Context) (id xid.ID, ok bool)
- func IDFromRequest(r *http.Request) (id xid.ID, ok bool)
- func MethodHandler(fieldKey string) func(next http.Handler) http.Handler
- func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler
- func RefererHandler(fieldKey string) func(next http.Handler) http.Handler
- func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler
- func RequestHandler(fieldKey string) func(next http.Handler) http.Handler
- func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.Handler
- func TraceIDFromCtx(ctx context.Context) (id string, ok bool)
- func URLHandler(fieldKey string) func(next http.Handler) http.Handler
- func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler
- func WithValue(ctx context.Context, reqID xid.ID) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessHandler ¶
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler
AccessHandler returns a handler that call f after each request.
func ContextTransfer ¶ added in v0.2.8
func CustomHeaderHandler ¶
CustomHeaderHandler adds given header from request's header as a field to the context's logger using fieldKey as field key.
func FromRequest ¶
FromRequest gets the logger in the request's context. This is a shortcut for log.Ctx(r.Context())
func IDFromRequest ¶
IDFromRequest returns the unique id associated to the request if any.
func MethodHandler ¶
MethodHandler adds the request method as a field to the context's logger using fieldKey as field key.
func NewHandler ¶
NewHandler injects log into requests context.
func RefererHandler ¶
RefererHandler adds the request's referer as a field to the context's logger using fieldKey as field key.
func RemoteAddrHandler ¶
RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.
func RequestHandler ¶
RequestHandler adds the request method and URL as a field to the context's logger using fieldKey as field key.
func RequestIDHandler ¶
RequestIDHandler returns a handler setting a unique id to the request which can be gathered using IDFromRequest(req). This generated id is added as a field to the logger using the passed fieldKey as field name. The id is also added as a response header if the headerName is not empty.
The generated id is a URL safe base64 encoded mongo object-id-like unique id. Mongo unique id generation algorithm has been selected as a trade-off between size and ease of use: UUID is less space efficient and snowflake requires machine configuration.
func TraceIDFromCtx ¶ added in v0.1.98
TraceIDFromCtx returns the trace id associated to the context if any.
func URLHandler ¶
URLHandler adds the requested URL as a field to the context's logger using fieldKey as field key.
func UserAgentHandler ¶
UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.
Types ¶
This section is empty.