Documentation
¶
Overview ¶
Package hlog provides HTTP middleware for structured logging of requests and responses.
It implements endpoint.LogMiddleware and integrates with the go-kit HTTP framework.
This middleware logs HTTP method, URL, and optionally the request and response bodies, based on the provided configuration or default options. It supports logging body content for the following content types: "application/json" and "text/xml" by default.
There are two ways to use the middleware:
- Use Log() for simple body logging toggle
- Use LogWithOptions() for fine-grained control via functional options
Example usage:
mux := http.NewServeMux()
handler := hlog.Log(logger, true)(yourHandler)
mux.Handle("/endpoint", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = handler(context.Background(), w, r)
}))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Log ¶
func Log(logger log.Logger, logBody bool) endpoint.LogMiddleware
Log returns a logging middleware that logs HTTP request and response metadata. If logBody is true, it also logs the request and response bodies for supported content types ("application/json", "text/xml").
func LogWithOptions ¶
func LogWithOptions(logger log.Logger, opts ...Option) endpoint.LogMiddleware
LogWithOptions returns a logging middleware with fine-grained configuration. Use this when you want to selectively enable request/response body logging or customize the content types for which body logging is enabled.
func Noop ¶
func Noop() endpoint.LogMiddleware
Types ¶
type Option ¶
type Option func(cfg *logConfig)