Skip to content

Commit

Permalink
enable passing custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjenicek committed Apr 2, 2024
1 parent 6c51581 commit 438c87a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
52 changes: 32 additions & 20 deletions logRequests.go
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)
})
}
}
3 changes: 1 addition & 2 deletions setHeaderFunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"
"testing"
"time"

"github.com/go-chi/transport"
"golang.org/x/sync/errgroup"
)
Expand All @@ -34,7 +33,7 @@ func TestSetHeaderFunc(t *testing.T) {
Transport: transport.Chain(
http.DefaultTransport,
transport.SetHeaderFunc("Authorization", issueRandomAuthToken),
transport.LogRequests,
transport.LogRequests(nil),
),
Timeout: 15 * time.Second,
}
Expand Down
3 changes: 1 addition & 2 deletions setHeader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"testing"
"time"

"github.com/go-chi/transport"
)

Expand Down Expand Up @@ -45,7 +44,7 @@ func TestSetHeader(t *testing.T) {
transport.SetHeader("User-Agent", userAgent),
transport.SetHeader("Authorization", authHeader),
transport.SetHeader("x-extra", "value"),
transport.LogRequests,
transport.LogRequests(nil),
),
Timeout: 15 * time.Second,
}
Expand Down
8 changes: 7 additions & 1 deletion transport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package transport

import "net/http"
import (
"net/http"
)

type Logger interface {
Info(format string, v ...interface{})
}

// RoundTripFunc, similar to http.HandlerFunc, is an adapter
// to allow the use of ordinary functions as http.RoundTrippers.
Expand Down
21 changes: 11 additions & 10 deletions transport_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package transport
package transport_test

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-chi/transport"
)

func TestChain(t *testing.T) {
Expand All @@ -21,10 +22,10 @@ func TestChain(t *testing.T) {

client := &http.Client{
Timeout: 15 * time.Second,
Transport: Chain(
Transport: transport.Chain(
nil,
SetHeader("User-Agent", "transport-chain/v1.0.0"),
LogRequests,
transport.SetHeader("User-Agent", "transport-chain/v1.0.0"),
transport.LogRequests(nil),
),
}

Expand Down Expand Up @@ -60,10 +61,10 @@ func TestChainWithRetries(t *testing.T) {

client := &http.Client{
Timeout: 15 * time.Second,
Transport: Chain(
Transport: transport.Chain(
http.DefaultTransport,
Retry(http.DefaultTransport, 5),
LogRequests,
transport.Retry(http.DefaultTransport, 5),
transport.LogRequests(nil),
),
}

Expand Down Expand Up @@ -100,10 +101,10 @@ func TestChainWithRetryAfter(t *testing.T) {

client := &http.Client{
Timeout: 15 * time.Second,
Transport: Chain(
Transport: transport.Chain(
http.DefaultTransport,
Retry(http.DefaultTransport, 5),
LogRequests,
transport.Retry(http.DefaultTransport, 5),
transport.LogRequests(nil),
),
}

Expand Down

0 comments on commit 438c87a

Please sign in to comment.