Skip to content

Commit

Permalink
Added interface for custom loggers
Browse files Browse the repository at this point in the history
This interface should allow to plug-in custom loggers
more easily.
  • Loading branch information
harnash committed Nov 10, 2018
1 parent e651d75 commit f976417
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,24 @@ func NewRequest(method, url string, rawBody interface{}) (*Request, error) {
return &Request{body, httpReq}, nil
}

// Logger interface allows to use other loggers than
// standard log.Logger.
type Logger interface {
Printf(string, ...interface{})
}

// RequestLogHook allows a function to run before each retry. The HTTP
// request which will be made, and the retry number (0 for the initial
// request) are available to users. The internal logger is exposed to
// consumers.
type RequestLogHook func(*log.Logger, *http.Request, int)
type RequestLogHook func(Logger, *http.Request, int)

// ResponseLogHook is like RequestLogHook, but allows running a function
// on each HTTP response. This function will be invoked at the end of
// every HTTP request executed, regardless of whether a subsequent retry
// needs to be performed or not. If the response body is read or closed
// from this method, this will affect the response returned from Do().
type ResponseLogHook func(*log.Logger, *http.Response)
type ResponseLogHook func(Logger, *http.Response)

// CheckRetry specifies a policy for handling retries. It is called
// following each request with the response and error values returned by
Expand All @@ -221,7 +227,7 @@ type ErrorHandler func(resp *http.Response, err error, numTries int) (*http.Resp
// like automatic retries to tolerate minor outages.
type Client struct {
HTTPClient *http.Client // Internal HTTP client.
Logger *log.Logger // Customer logger instance.
Logger Logger // Customer logger instance.

RetryWaitMin time.Duration // Minimum time to wait
RetryWaitMax time.Duration // Maximum time to wait
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func testClient_Do(t *testing.T, body interface{}) {
client.RetryWaitMin = 10 * time.Millisecond
client.RetryWaitMax = 50 * time.Millisecond
client.RetryMax = 50
client.RequestLogHook = func(logger *log.Logger, req *http.Request, retryNumber int) {
client.RequestLogHook = func(logger Logger, req *http.Request, retryNumber int) {
retryCount = retryNumber

if logger != client.Logger {
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestClient_RequestLogHook(t *testing.T) {
testURIPath := "/foo/bar"

client := NewClient()
client.RequestLogHook = func(logger *log.Logger, req *http.Request, retry int) {
client.RequestLogHook = func(logger Logger, req *http.Request, retry int) {
retries = retry

if logger != client.Logger {
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestClient_ResponseLogHook(t *testing.T) {
client.RetryWaitMin = 10 * time.Millisecond
client.RetryWaitMax = 10 * time.Millisecond
client.RetryMax = 15
client.ResponseLogHook = func(logger *log.Logger, resp *http.Response) {
client.ResponseLogHook = func(logger Logger, resp *http.Response) {
if resp.StatusCode == 200 {
// Log something when we get a 200
logger.Printf("test_log_pass")
Expand Down

0 comments on commit f976417

Please sign in to comment.