-
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.
- Loading branch information
lukasjenicek
committed
Apr 2, 2024
1 parent
6c51581
commit 438c87a
Showing
5 changed files
with
52 additions
and
35 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,42 @@ | ||
package transport | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"time" | ||
"log" | ||
|
||
"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) | ||
|
||
curlCommand, _ := http2curl.GetCurlCommand(r) | ||
log.Printf("%v", curlCommand) | ||
log.Printf("request: %s %s", r.Method, r.URL) | ||
|
||
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) | ||
} | ||
}() | ||
|
||
return next.RoundTrip(r) | ||
}) | ||
type DefaultLogger struct{} | ||
|
||
func (*DefaultLogger) Info(format string, v ...interface{}) { | ||
log.Printf(format, v...) | ||
} | ||
|
||
func LogRequests(logger Logger) func(http.RoundTripper) http.RoundTripper { | ||
if logger == nil { | ||
logger = &DefaultLogger{} | ||
} | ||
|
||
return func(next http.RoundTripper) http.RoundTripper { | ||
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) { | ||
r := cloneRequest(req) | ||
|
||
curlCommand, _ := http2curl.GetCurlCommand(r) | ||
logger.Info("%v", curlCommand) | ||
logger.Info("request: %s %s", r.Method, r.URL) | ||
|
||
startTime := time.Now() | ||
defer func() { | ||
if resp != nil { | ||
logger.Info("response (HTTP %v): %v %s", time.Since(startTime), resp.Status, r.URL) | ||
} else { | ||
logger.Info("response (<nil>): %v %s", time.Since(startTime), r.URL) | ||
} | ||
}() | ||
|
||
return next.RoundTrip(r) | ||
}) | ||
} | ||
} |
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
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
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
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