reqpretty is a Go middleware package designed to beautify and log HTTP requests and responses in a structured and readable format. It provides detailed logging of request and response headers, bodies, query parameters, and more, with customization options to suit your needs.
| Feature | Description |
|---|---|
| 📥 Request Logging | Log HTTP method, URL, headers, query parameters, and body |
| 📤 Response Logging | Log HTTP status code, headers, and body |
| ⚙️ Customization | Configure what details to log, including request and response headers, bodies, and query parameters |
| 🔍 Context Attributes | Extract and log specific context attributes |
| 🌈 Colorized Output | Optionally colorize log output for better readability |
reqpretty works by providing an HTTP middleware (DebugHandler) that wraps your existing http.Handler. Here’s a step-by-step breakdown of the process:
- Wrap Handler: The
DebugHandlertakes your handler and anOptionsstruct as input. - Intercept Request: When a request comes in, the middleware intercepts it before it reaches your main handler.
- Capture Request Body: It reads the request body into a buffer and then restores it, ensuring that your handler can still read the body as normal.
- Wrap ResponseWriter: It uses a custom
http.ResponseWriterto capture the status code and response body that your handler writes. - Process Request: It passes the request to your main handler for processing.
- Log Everything: Once your handler has finished, the middleware logs the complete request and response details in a beautiful, box-style format based on the options you configured. The time taken to process the request is also calculated and logged.
go get github.com/1saif/reqprettyHere's a simple example of how to use reqpretty in your Go application.
First, configure the logger:
package main
import (
"log/slog"
"github.com/1saifj/reqpretty"
)
func main() {
logger := &reqpretty.Logger{}
reqpretty.Configure(logger)
// Your application code
}Next, use the reqpretty middleware in your HTTP server:
package main
import (
"net/http"
"github.com/1saifj/reqpretty"
)
func main() {
opts := reqpretty.Options{
IncludeRequest: true,
IncludeRequestHeaders: true,
IncludeRequestQueryParams: true,
IncludeRequestBody: true,
IncludeResponse: true,
IncludeResponseHeaders: true,
IncludeResponseBody: true,
ContextAttributes: []string{"request_id", "user_id"},
}
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
loggedMux := reqpretty.DebugHandler(opts, mux)
http.ListenAndServe(":8080", loggedMux)
}The Options struct allows you to customize what details are logged:
| Option | Type | Default | Description |
|---|---|---|---|
IncludeRequest |
bool |
false |
Log the request details |
IncludeRequestHeaders |
bool |
false |
Log request headers |
IncludeRequestQueryParams |
bool |
false |
Log request query parameters |
IncludeRequestBody |
bool |
false |
Log request body |
IncludeResponse |
bool |
false |
Log the response details |
IncludeResponseHeaders |
bool |
false |
Log response headers |
IncludeResponseBody |
bool |
false |
Log response body |
ContextAttributes |
[]string |
nil |
List of context attributes to log |
The Logger struct is used to configure the logger:
clone(): Create a copy of the logger
You can customize the logger further by implementing your own slog.Handler:
Click to expand custom logger example
package main
import (
"context"
"log/slog"
"github.com/1saifj/reqpretty"
)
type CustomHandler struct {
handler slog.Handler
}
func (h CustomHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.handler.Enabled(ctx, level)
}
func (h CustomHandler) Handle(ctx context.Context, record slog.Record) error {
// Custom log handling
return h.handler.Handle(ctx, record)
}
func (h CustomHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return CustomHandler{handler: h.handler.WithAttrs(attrs)}
}
func (h CustomHandler) WithGroup(name string) slog.Handler {
return CustomHandler{handler: h.handler.WithGroup(name)}
}
func main() {
logger := &reqpretty.Logger{}
reqpretty.Configure(logger)
customHandler := CustomHandler{handler: slog.Default().Handler()}
slog.SetDefault(slog.New(customHandler))
// Your application code
}Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
Inspired by the pretty_dio_logger for Dart.
Made with ❤️ by the 1saifj
⭐ Star this repo if you find it helpful! ⭐
