Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable passing custom logger #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Comment on lines +17 to 42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.. however instead of Logger can we just use io.Writer?

I found myself needing this, so I could save each request to a buffer and only log/print it on error :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally forgot that I created this PR and today I created similar PR just week after :D It's the heat I guess..

Anyway what do you think about this approach: #7 ???

This way the consumer of the package can do whatever he likes

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
Loading