Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log prefix enhancement #116

Merged
merged 2 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,15 @@ 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())

c.disableLogPrefix()
c.enableLogPrefix()
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