Skip to content

Commit

Permalink
LeveledLogger: Add doc and prioritize it over Logger (#97)
Browse files Browse the repository at this point in the history
* Document LeveledLogger better to make it clear to developers that the
  second variadic argument accepts key-value pairs like hci-log or zap
  logger and not Printf style logging.
* When the passed logger implements both Logger and LeveledLogger
  interfaces, prioritize LeveledLogger over Logger. Switch statements
  evaluate cases from top to bottom.

Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
  • Loading branch information
ppai-plivo and jefferai authored Jun 15, 2020
1 parent 171d0c9 commit cf855b1
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"sync"
"time"

"github.com/hashicorp/go-cleanhttp"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)

var (
Expand Down Expand Up @@ -276,12 +276,16 @@ type Logger interface {
Printf(string, ...interface{})
}

// LeveledLogger interface implements the basic methods that a logger library needs
// LeveledLogger is an interface that can be implemented by any logger or a
// logger wrapper to provide leveled logging. The methods accept a message
// string and a variadic number of key-value pairs. For log.Printf style
// formatting where message string contains a format specifier, use Logger
// interface.
type LeveledLogger interface {
Error(string, ...interface{})
Info(string, ...interface{})
Debug(string, ...interface{})
Warn(string, ...interface{})
Error(msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
Debug(msg string, keysAndValues ...interface{})
Warn(msg string, keysAndValues ...interface{})
}

// hookLogger adapts an LeveledLogger to Logger for use by the existing hook functions
Expand Down Expand Up @@ -543,10 +547,10 @@ func (c *Client) Do(req *Request) (*http.Response, error) {

if logger != nil {
switch v := logger.(type) {
case Logger:
v.Printf("[DEBUG] %s %s", req.Method, req.URL)
case LeveledLogger:
v.Debug("performing request", "method", req.Method, "url", req.URL)
case Logger:
v.Printf("[DEBUG] %s %s", req.Method, req.URL)
}
}

Expand Down Expand Up @@ -576,10 +580,10 @@ func (c *Client) Do(req *Request) (*http.Response, error) {

if c.RequestLogHook != nil {
switch v := logger.(type) {
case Logger:
c.RequestLogHook(v, req.Request, i)
case LeveledLogger:
c.RequestLogHook(hookLogger{v}, req.Request, i)
case Logger:
c.RequestLogHook(v, req.Request, i)
default:
c.RequestLogHook(nil, req.Request, i)
}
Expand All @@ -596,21 +600,21 @@ func (c *Client) Do(req *Request) (*http.Response, error) {

if doErr != nil {
switch v := logger.(type) {
case Logger:
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, doErr)
case LeveledLogger:
v.Error("request failed", "error", doErr, "method", req.Method, "url", req.URL)
case Logger:
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,
// even if CheckRetry signals to stop.
if c.ResponseLogHook != nil {
// Call the response logger function if provided.
switch v := logger.(type) {
case Logger:
c.ResponseLogHook(v, resp)
case LeveledLogger:
c.ResponseLogHook(hookLogger{v}, resp)
case Logger:
c.ResponseLogHook(v, resp)
default:
c.ResponseLogHook(nil, resp)
}
Expand Down Expand Up @@ -640,10 +644,10 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
}
if logger != nil {
switch v := logger.(type) {
case Logger:
v.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, wait, remain)
case LeveledLogger:
v.Debug("retrying request", "request", desc, "timeout", wait, "remaining", remain)
case Logger:
v.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, wait, remain)
}
}
select {
Expand Down Expand Up @@ -694,10 +698,10 @@ func (c *Client) drainBody(body io.ReadCloser) {
if err != nil {
if c.logger() != nil {
switch v := c.logger().(type) {
case Logger:
v.Printf("[ERR] error reading response body: %v", err)
case LeveledLogger:
v.Error("error reading response body", "error", err)
case Logger:
v.Printf("[ERR] error reading response body: %v", err)
}
}
}
Expand Down

0 comments on commit cf855b1

Please sign in to comment.