Skip to content

Commit

Permalink
#113 log prefix enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 21, 2018
1 parent 9ba6ff5 commit 33b8e15
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
10 changes: 9 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type Client struct {
closeConnection bool
notParseResponse bool
debugBodySizeLimit int64
logPrefix string
beforeRequest []func(*Client, *Request) error
udBeforeRequest []func(*Client, *Request) error
preReqHook func(*Client, *Request) error
Expand Down Expand Up @@ -711,6 +712,13 @@ 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
}

// IsProxySet method returns the true if proxy is set on client otherwise false.
func (c *Client) IsProxySet() bool {
return c.proxyURL != nil
Expand Down Expand Up @@ -791,7 +799,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
// enables a log prefix
func (c *Client) enableLogPrefix() {
c.Log.SetFlags(log.LstdFlags)
c.Log.SetPrefix("RESTY ")
c.Log.SetPrefix(c.logPrefix)
}

// disables a log prefix
Expand Down
7 changes: 7 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,10 @@ type CustomRoundTripper struct {
func (rt *CustomRoundTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
return &http.Response{}, nil
}

func TestSetLogPrefix(t *testing.T) {
c := New()
c.SetLogPrefix("CUSTOM ")
assertEqual(t, "CUSTOM ", c.logPrefix)
assertEqual(t, "CUSTOM ", c.Log.Prefix())
}
3 changes: 2 additions & 1 deletion default.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func IsProxySet() bool {

// GetClient method returns the current `http.Client` used by the default resty client.
func GetClient() *http.Client {
return DefaultClient.httpClient
return DefaultClient.httpClient
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand All @@ -284,6 +284,7 @@ func createClient(hc *http.Client) *Client {
JSONUnmarshal: json.Unmarshal,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
logPrefix: "RESTY ",
}

// Default transport
Expand Down
6 changes: 3 additions & 3 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,9 @@ func TestSRV(t *testing.T) {
assertEqual(t, "google.com", r.SRV.Domain)

resp, err := r.Get("/")
assertError(t, err)
assertNotNil(t, resp)
if resp != nil {
if err == nil {
assertError(t, err)
assertNotNil(t, resp)
assertEqual(t, http.StatusOK, resp.StatusCode())
}
}
Expand Down

0 comments on commit 33b8e15

Please sign in to comment.