-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add possibility to modify logging of http request and reponse
- Loading branch information
1 parent
a2be5e0
commit 31dc47e
Showing
1 changed file
with
57 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,71 @@ | ||
package transport | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"moul.io/http2curl/v2" | ||
) | ||
|
||
func LogRequests(next http.RoundTripper) http.RoundTripper { | ||
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) { | ||
r := CloneRequest(req) | ||
type RequestLogger interface { | ||
LogRequest(req *http.Request, curl *http2curl.CurlCommand) | ||
LogResponse(r *http.Request, resp *http.Response, startTime time.Time) | ||
} | ||
|
||
curlCommand, _ := http2curl.GetCurlCommand(r) | ||
log.Printf("%v", curlCommand) | ||
log.Printf("request: %s %s", r.Method, r.URL) | ||
func LogRequests(logger RequestLogger) func(next http.RoundTripper) http.RoundTripper { | ||
return func(next http.RoundTripper) http.RoundTripper { | ||
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) { | ||
r := CloneRequest(req) | ||
|
||
startTime := time.Now() | ||
defer func() { | ||
if resp != nil { | ||
log.Printf("response (HTTP %v): %v %s", time.Since(startTime), resp.Status, r.URL) | ||
} else { | ||
log.Printf("response (<nil>): %v %s", time.Since(startTime), r.URL) | ||
} | ||
}() | ||
curlCommand, _ := http2curl.GetCurlCommand(r) | ||
|
||
logger.LogRequest(req, curlCommand) | ||
|
||
startTime := time.Now() | ||
defer func() { | ||
logger.LogResponse(r, resp, startTime) | ||
}() | ||
|
||
return next.RoundTrip(r) | ||
}) | ||
} | ||
} | ||
|
||
type DefaultLogger struct { | ||
PrintResponsePayload bool | ||
} | ||
|
||
return next.RoundTrip(r) | ||
}) | ||
func (d *DefaultLogger) LogRequest(r *http.Request, curl *http2curl.CurlCommand) { | ||
log.Printf(curl.String()) | ||
log.Printf("request: %s %s", r.Method, r.URL) | ||
} | ||
|
||
func (d *DefaultLogger) LogResponse(r *http.Request, resp *http.Response, startTime time.Time) { | ||
if resp == nil { | ||
log.Printf(fmt.Sprintf("response (<nil>): %v %s", time.Since(startTime), r.URL)) | ||
return | ||
} | ||
|
||
log.Printf("response: %s %s", resp.Status, resp.Request.URL) | ||
|
||
if d.PrintResponsePayload && resp.Header.Get("Content-Type") == "application/json" { | ||
var b bytes.Buffer | ||
|
||
tee := io.TeeReader(resp.Body, &b) | ||
resp.Body = io.NopCloser(&b) | ||
|
||
payload, err := io.ReadAll(tee) | ||
if err == nil { | ||
// Pretty print the JSON payload | ||
var prettyJSON bytes.Buffer | ||
if err := json.Indent(&prettyJSON, payload, "", " "); err == nil { | ||
log.Printf("%s", prettyJSON.String()) | ||
} | ||
} | ||
} | ||
} |