Skip to content

Commit 80b9e2b

Browse files
feat(client): add debug log helper
1 parent 853a2c0 commit 80b9e2b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

option/middleware.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
package option
4+
5+
import (
6+
"log"
7+
"net/http"
8+
"net/http/httputil"
9+
)
10+
11+
// WithDebugLog logs the HTTP request and response content.
12+
// If the logger parameter is nil, it uses the default logger.
13+
//
14+
// WithDebugLog is for debugging and development purposes only.
15+
// It should not be used in production code. The behavior and interface
16+
// of WithDebugLog is not guaranteed to be stable.
17+
func WithDebugLog(logger *log.Logger) RequestOption {
18+
return WithMiddleware(func(req *http.Request, nxt MiddlewareNext) (*http.Response, error) {
19+
if logger == nil {
20+
logger = log.Default()
21+
}
22+
23+
if reqBytes, err := httputil.DumpRequest(req, true); err == nil {
24+
logger.Printf("Request Content:\n%s\n", reqBytes)
25+
}
26+
27+
resp, err := nxt(req)
28+
if err != nil {
29+
return resp, err
30+
}
31+
32+
if respBytes, err := httputil.DumpResponse(resp, true); err == nil {
33+
logger.Printf("Response Content:\n%s\n", respBytes)
34+
}
35+
36+
return resp, err
37+
})
38+
}

0 commit comments

Comments
 (0)