Skip to content

Commit

Permalink
only pass context to LeveledLogger (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-shatokhin authored Dec 16, 2021
1 parent 4a705a0 commit 6d171cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func NewRequestWithContext(ctx context.Context, method, url string, rawBody inte
// Logger interface allows to use other loggers than
// standard log.Logger.
type Logger interface {
Printf(context.Context, string, ...interface{})
Printf(string, ...interface{})
}

// LeveledLogger is an interface that can be implemented by any logger or a
Expand Down Expand Up @@ -315,8 +315,8 @@ type hookLogger struct {
LeveledLogger
}

func (h hookLogger) Printf(ctx context.Context, s string, args ...interface{}) {
h.Info(ctx, fmt.Sprintf(s, args...))
func (h hookLogger) Printf(s string, args ...interface{}) {
h.Info(context.Background(), fmt.Sprintf(s, args...))
}

// RequestLogHook allows a function to run before each retry. The HTTP
Expand Down Expand Up @@ -570,7 +570,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
case LeveledLogger:
v.Debug(ctx, "performing request", "method", req.Method, "url", req.URL)
case Logger:
v.Printf(ctx, "[DEBUG] %s %s", req.Method, req.URL)
v.Printf("[DEBUG] %s %s", req.Method, req.URL)
}
}

Expand Down Expand Up @@ -618,7 +618,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
case LeveledLogger:
v.Error(ctx, "request failed", "error", doErr, "method", req.Method, "url", req.URL)
case Logger:
v.Printf(ctx, "[ERR] %s %s request failed: %v", req.Method, req.URL, doErr)
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, doErr)
}
} else {
// Call this here to maintain the behavior of logging all requests,
Expand Down Expand Up @@ -662,7 +662,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
case LeveledLogger:
v.Debug(ctx, "retrying request", "request", desc, "timeout", wait, "remaining", remain)
case Logger:
v.Printf(ctx, "[DEBUG] %s: retrying in %s (%d left)", desc, wait, remain)
v.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, wait, remain)
}
}
timer := time.NewTimer(wait)
Expand Down Expand Up @@ -723,7 +723,7 @@ func (c *Client) drainBody(ctx context.Context, body io.ReadCloser) {
case LeveledLogger:
v.Error(ctx, "error reading response body", "error", err)
case Logger:
v.Printf(ctx, "[ERR] error reading response body: %v", err)
v.Printf("[ERR] error reading response body: %v", err)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ func TestClient_ResponseLogHook(t *testing.T) {
}

func testClientResponseLogHook(t *testing.T, l interface{}, buf *bytes.Buffer) {
ctx := context.Background()
passAfter := time.Now().Add(100 * time.Millisecond)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if time.Now().After(passAfter) {
Expand All @@ -412,7 +411,7 @@ func testClientResponseLogHook(t *testing.T, l interface{}, buf *bytes.Buffer) {
successLog := "test_log_pass"
// Log something when we get a 200
if logger != nil {
logger.Printf(ctx, successLog)
logger.Printf(successLog)
} else {
buf.WriteString(successLog)
}
Expand All @@ -424,7 +423,7 @@ func testClientResponseLogHook(t *testing.T, l interface{}, buf *bytes.Buffer) {
}
failLog := string(body)
if logger != nil {
logger.Printf(ctx, failLog)
logger.Printf(failLog)
} else {
buf.WriteString(failLog)
}
Expand Down

0 comments on commit 6d171cc

Please sign in to comment.