httplog icon indicating copy to clipboard operation
httplog copied to clipboard

Configurable HTTP Response Logging

Open jguttman94 opened this issue 2 years ago • 0 comments

Currently, the response body will only be logged if the returned status code is >= 400.

https://github.com/go-chi/httplog/blob/master/httplog.go#L90C4-L96C7. https://github.com/go-chi/httplog/blob/master/httplog.go#L146C3-L149C4.

I have a use case for httplog to act as an audit capability that logs all requests and responses to/from the system. With the current implementation, the response body is only "auditable" if the server processes the request with an error.

My suggestion is to include a new configurable option that would be used for the condition rather than status >= 400. The option can default to the existing logic but allow consumers to define if they want response bodies to be logged in different situations.

if l.Options.ResponseBody {
	body, _ := extra.([]byte)
	responseLog = append(responseLog, slog.Attr{Key: "body", Value: slog.StringValue(string(body))})
}

jguttman94 avatar Jan 08 '24 22:01 jguttman94