go-chi / httplog

Go HTTP request logger with structured logging capabilities built on "log/slog" package
MIT License
207 stars 42 forks source link

Configurable HTTP Response Logging #36

Open jguttman94 opened 10 months ago

jguttman94 commented 10 months ago

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))})
}