Skip to content

Commit

Permalink
#229 resty logger becomes interface to provides choice to an user and…
Browse files Browse the repository at this point in the history
… also provides default stub to get start
  • Loading branch information
jeevatkm committed Mar 13, 2019
1 parent 97a5dbf commit 9d4440d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Auto detects file content type
* Request URL [Path Params (aka URI Params)](https://godoc.org/github.com/go-resty/resty#Request.SetPathParams)
* Backoff Retry Mechanism with retry condition function [reference](retry_test.go)
* resty client HTTP & REST [Request](https://godoc.org/github.com/go-resty/resty#Client.OnBeforeRequest) and [Response](https://godoc.org/github.com/go-resty/resty#Client.OnAfterResponse) middlewares
* Resty client HTTP & REST [Request](https://godoc.org/github.com/go-resty/resty#Client.OnBeforeRequest) and [Response](https://godoc.org/github.com/go-resty/resty#Client.OnAfterResponse) middlewares
* `Request.SetContext` supported
* Authorization option of `BasicAuth` and `Bearer` token
* Set request `ContentLength` value for all request or particular request
Expand Down
54 changes: 24 additions & 30 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"runtime"
Expand Down Expand Up @@ -87,7 +85,6 @@ type Client struct {
Debug bool
DisableWarn bool
AllowGetMethodPayload bool
Log *log.Logger
RetryCount int
RetryWaitTime time.Duration
RetryMaxWaitTime time.Duration
Expand All @@ -103,8 +100,8 @@ type Client struct {
debugBodySizeLimit int64
outputDirectory string
scheme string
logPrefix string
pathParams map[string]string
log Logger
httpClient *http.Client
proxyURL *url.URL
beforeRequest []func(*Client, *Request) error
Expand Down Expand Up @@ -368,7 +365,7 @@ func (c *Client) OnAfterResponse(m func(*Client, *Response) error) *Client {
// Note: Only one pre-request hook can be registered. Use `client.OnBeforeRequest` for mutilple.
func (c *Client) SetPreRequestHook(h func(*Client, *http.Request) error) *Client {
if c.preReqHook != nil {
c.Log.Printf("Overwriting an existing pre-request hook: %s", functionName(h))
c.log.Warnf("Overwriting an existing pre-request hook: %s", functionName(h))
}
c.preReqHook = h
return c
Expand All @@ -394,7 +391,8 @@ func (c *Client) SetDebugBodyLimit(sl int64) *Client {
// called before the resty actually logs the information.
func (c *Client) OnRequestLog(rl func(*RequestLog) error) *Client {
if c.requestLog != nil {
c.Log.Printf("Overwriting an existing on-request-log callback from=%s to=%s", functionName(c.requestLog), functionName(rl))
c.log.Warnf("Overwriting an existing on-request-log callback from=%s to=%s",
functionName(c.requestLog), functionName(rl))
}
c.requestLog = rl
return c
Expand All @@ -404,7 +402,8 @@ func (c *Client) OnRequestLog(rl func(*RequestLog) error) *Client {
// called before the resty actually logs the information.
func (c *Client) OnResponseLog(rl func(*ResponseLog) error) *Client {
if c.responseLog != nil {
c.Log.Printf("Overwriting an existing on-response-log callback from=%s to=%s", functionName(c.responseLog), functionName(rl))
c.log.Warnf("Overwriting an existing on-response-log callback from=%s to=%s",
functionName(c.responseLog), functionName(rl))
}
c.responseLog = rl
return c
Expand All @@ -429,12 +428,10 @@ func (c *Client) SetAllowGetMethodPayload(a bool) *Client {
}

// SetLogger method sets given writer for logging Resty request and response details.
// Defaults to os.Stderr.
// file, _ := os.OpenFile("/Users/jeeva/go-resty.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
//
// client.SetLogger(file)
func (c *Client) SetLogger(w io.Writer) *Client {
c.Log = getLogger(w)
// Compliant to interface `resty.Logger`.
func (c *Client) SetLogger(l Logger) *Client {
c.log = l
return c
}

Expand Down Expand Up @@ -476,7 +473,7 @@ func (c *Client) SetError(err interface{}) *Client {
func (c *Client) SetRedirectPolicy(policies ...interface{}) *Client {
for _, p := range policies {
if _, ok := p.(RedirectPolicy); !ok {
c.Log.Printf("ERORR: %v does not implement resty.RedirectPolicy (missing Apply method)",
c.log.Errorf("%v does not implement resty.RedirectPolicy (missing Apply method)",
functionName(p))
}
}
Expand Down Expand Up @@ -539,7 +536,7 @@ func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client {
func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
transport, err := c.transport()
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}
transport.TLSClientConfig = config
Expand All @@ -555,13 +552,13 @@ func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
func (c *Client) SetProxy(proxyURL string) *Client {
transport, err := c.transport()
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}

pURL, err := url.Parse(proxyURL)
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}

Expand All @@ -575,7 +572,7 @@ func (c *Client) SetProxy(proxyURL string) *Client {
func (c *Client) RemoveProxy() *Client {
transport, err := c.transport()
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}
c.proxyURL = nil
Expand All @@ -587,7 +584,7 @@ func (c *Client) RemoveProxy() *Client {
func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
config, err := c.tlsConfig()
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}
config.Certificates = append(config.Certificates, certs...)
Expand All @@ -599,13 +596,13 @@ func (c *Client) SetCertificates(certs ...tls.Certificate) *Client {
func (c *Client) SetRootCertificate(pemFilePath string) *Client {
rootPemData, err := ioutil.ReadFile(pemFilePath)
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}

config, err := c.tlsConfig()
if err != nil {
c.Log.Printf("ERROR %v", err)
c.log.Errorf("%v", err)
return c
}
if config.RootCAs == nil {
Expand Down Expand Up @@ -677,13 +674,6 @@ func (c *Client) SetDoNotParseResponse(parse bool) *Client {
return c
}

// SetLogPrefix method sets the Resty logger prefix value.
func (c *Client) SetLogPrefix(prefix string) *Client {
c.logPrefix = prefix
c.Log.SetPrefix(prefix)
return c
}

// SetPathParams method sets multiple URL path key-value pairs at one go in the
// Resty client instance.
// client.SetPathParams(map[string]string{
Expand Down Expand Up @@ -859,6 +849,11 @@ func (c *Client) transport() (*http.Transport, error) {
return nil, errors.New("current transport is not an *http.Transport instance")
}

// just an helper method
func (c *Client) outputLogTo(w io.Writer) {
c.log.(*logger).l.SetOutput(w)
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// File struct and its methods
//_______________________________________________________________________
Expand Down Expand Up @@ -901,7 +896,6 @@ func createClient(hc *http.Client) *Client {
FormData: url.Values{},
Header: http.Header{},
Cookies: make([]*http.Cookie, 0),
Log: getLogger(os.Stderr),
RetryWaitTime: defaultWaitTime,
RetryMaxWaitTime: defaultMaxWaitTime,
JSONMarshal: json.Marshal,
Expand All @@ -912,8 +906,8 @@ func createClient(hc *http.Client) *Client {
pathParams: make(map[string]string),
}

// Log Prefix
c.SetLogPrefix("RESTY ")
// Logger
c.SetLogger(createLogger())

// default before request middlewares
c.beforeRequest = []func(*Client, *Request) error{
Expand Down
16 changes: 7 additions & 9 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ func TestClientOptions(t *testing.T) {
assertEqual(t, true, transport.TLSClientConfig.InsecureSkipVerify)

client.OnBeforeRequest(func(c *Client, r *Request) error {
c.Log.Println("I'm in Request middleware")
c.log.Debugf("I'm in Request middleware")
return nil // if it success
})
client.OnAfterResponse(func(c *Client, r *Response) error {
c.Log.Println("I'm in Response middleware")
c.log.Debugf("I'm in Response middleware")
return nil // if it success
})

Expand All @@ -344,19 +344,17 @@ func TestClientOptions(t *testing.T) {

client.SetCloseConnection(true)
assertEqual(t, client.closeConnection, true)

client.SetLogger(ioutil.Discard)
}

func TestClientPreRequestHook(t *testing.T) {
client := dc()
client.SetPreRequestHook(func(c *Client, r *http.Request) error {
c.Log.Println("I'm in Pre-Request Hook")
c.log.Debugf("I'm in Pre-Request Hook")
return nil
})

client.SetPreRequestHook(func(c *Client, r *http.Request) error {
c.Log.Println("I'm Overwriting existing Pre-Request Hook")
c.log.Debugf("I'm Overwriting existing Pre-Request Hook")
return nil
})
}
Expand All @@ -379,7 +377,7 @@ func TestClientAllowsGetMethodPayload(t *testing.T) {

func TestClientRoundTripper(t *testing.T) {
c := NewWithClient(&http.Client{})
c.SetLogger(ioutil.Discard)
c.outputLogTo(ioutil.Discard)

rt := &CustomRoundTripper{}
c.SetTransport(rt)
Expand Down Expand Up @@ -409,8 +407,8 @@ func TestDebugBodySizeLimit(t *testing.T) {
var lgr bytes.Buffer
c := dc()
c.SetDebug(true)
c.SetLogger(&lgr)
c.SetDebugBodyLimit(30)
c.outputLogTo(&lgr)

testcases := []struct{ url, want string }{
// Text, does not exceed limit.
Expand Down Expand Up @@ -479,7 +477,7 @@ func TestLogCallbacks(t *testing.T) {
c := New().SetDebug(true)

var lgr bytes.Buffer
c.SetLogger(&lgr)
c.outputLogTo(&lgr)

c.OnRequestLog(func(r *RequestLog) error {
// masking authorzation header
Expand Down
4 changes: 2 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func addCredentials(c *Client, r *Request) error {

if !c.DisableWarn {
if isBasicAuth && !strings.HasPrefix(r.URL, "https") {
c.Log.Println("WARNING - Using Basic Auth in HTTP mode is not secure.")
c.log.Warnf("Using Basic Auth in HTTP mode is not secure, use HTTPS")
}
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func responseLogger(c *Client, res *Response) error {
}
debugLog += "==============================================================================\n"

c.Log.Print(debugLog)
c.log.Debugf(debugLog)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (r *Request) SetQueryString(query string) *Request {
}
}
} else {
r.client.Log.Printf("ERROR %v", err)
r.client.log.Errorf("%v", err)
}
return r
}
Expand Down Expand Up @@ -574,7 +574,7 @@ func (r *Request) Execute(method, url string) (*Response, error) {

resp, err = r.client.execute(r)
if err != nil {
r.client.Log.Printf("ERROR %v, Attempt %v", err, attempt)
r.client.log.Errorf("%v, Attempt %v", err, attempt)
if r.ctx != nil && r.ctx.Err() != nil {
// stop Backoff from retrying request if request has been
// canceled by context
Expand Down
22 changes: 11 additions & 11 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func TestGetClientParamRequestParam(t *testing.T) {
c := dc()
c.SetQueryParam("client_param", "true").
SetQueryParams(map[string]string{"req_1": "jeeva", "req_3": "jeeva3"}).
SetDebug(true).
SetLogger(ioutil.Discard)
SetDebug(true)
c.outputLogTo(ioutil.Discard)

resp, err := c.R().
SetQueryParams(map[string]string{"req_1": "req 1 value", "req_2": "req 2 value"}).
Expand Down Expand Up @@ -498,8 +498,8 @@ func TestFormData(t *testing.T) {
c := dc()
c.SetFormData(map[string]string{"zip_code": "00000", "city": "Los Angeles"}).
SetContentLength(true).
SetDebug(true).
SetLogger(ioutil.Discard)
SetDebug(true)
c.outputLogTo(ioutil.Discard)

resp, err := c.R().
SetFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
Expand All @@ -520,9 +520,8 @@ func TestMultiValueFormData(t *testing.T) {
}

c := dc()
c.SetContentLength(true).
SetDebug(true).
SetLogger(ioutil.Discard)
c.SetContentLength(true).SetDebug(true)
c.outputLogTo(ioutil.Discard)

resp, err := c.R().
SetQueryParamsFromValues(v).
Expand All @@ -541,8 +540,8 @@ func TestFormDataDisableWarn(t *testing.T) {
c.SetFormData(map[string]string{"zip_code": "00000", "city": "Los Angeles"}).
SetContentLength(true).
SetDebug(true).
SetLogger(ioutil.Discard).
SetDisableWarn(true)
c.outputLogTo(ioutil.Discard)

resp, err := c.R().
SetFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
Expand Down Expand Up @@ -820,7 +819,8 @@ func TestPutJSONString(t *testing.T) {
return nil
})

client.SetDebug(true).SetLogger(ioutil.Discard)
client.SetDebug(true)
client.outputLogTo(ioutil.Discard)

resp, err := client.R().
SetHeaders(map[string]string{hdrContentTypeKey: jsonContentType, hdrAcceptKey: jsonContentType}).
Expand Down Expand Up @@ -1159,8 +1159,8 @@ func TestOutputFileWithBaseDirAndRelativePath(t *testing.T) {
client := dc().
SetRedirectPolicy(FlexibleRedirectPolicy(10)).
SetOutputDirectory(filepath.Join(getTestDataPath(), "dir-sample")).
SetDebug(true).
SetLogger(ioutil.Discard)
SetDebug(true)
client.outputLogTo(ioutil.Discard)

resp, err := client.R().
SetOutput("go-resty/test-img-success.png").
Expand Down
9 changes: 4 additions & 5 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ func createTestServer(fn func(w http.ResponseWriter, r *http.Request)) *httptest
}

func dc() *Client {
client := New()
client.SetLogger(ioutil.Discard)
return client
c := New()
c.outputLogTo(ioutil.Discard)
return c
}

func dcr() *Request {
Expand All @@ -552,8 +552,7 @@ func dcr() *Request {
func dclr() *Request {
c := dc()
c.SetDebug(true)
c.SetLogger(ioutil.Discard)

c.outputLogTo(ioutil.Discard)
return c.R()
}

Expand Down
Loading

0 comments on commit 9d4440d

Please sign in to comment.