From f6bd5b385e651f008ca2a283d256975684f446aa Mon Sep 17 00:00:00 2001 From: Anthony Poschen Date: Sat, 14 May 2016 15:39:35 +1000 Subject: [PATCH] CHANGE: Renamed 'SetLogFile' to 'SetLog' and changed the parameter from a string to io.Writer CHANGE: Changed logFile name and type to name Log of type io.Writer CHANGE: func log of client now uses new log object. --- client.go | 24 +++++++++--------------- types.go | 3 ++- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 515edf6..442ab5c 100644 --- a/client.go +++ b/client.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "net/http" "net/http/httputil" - "os" ) // NewClient returns new Client struct @@ -23,16 +22,15 @@ func NewClient(clientID string, secret string, APIBase string) (*Client, error) clientID, secret, APIBase, - "", + nil, nil, }, nil } -// SetLogFile will set/change a full path to a log file -// If log file is set paypalsdk will log all requests and responses to this file -func (c *Client) SetLogFile(filepath string) error { - c.LogFile = filepath - +// SetLog will set/change the output destination. +// If log file is set paypalsdk will log all requests and responses to this Writer +func (c *Client) SetLog(log io.Writer) error { + c.Log = log return nil } @@ -119,15 +117,11 @@ func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Requ // log will dump request and response to the log file func (c *Client) log(req *http.Request, resp *http.Response) { - if c.LogFile != "" { - os.OpenFile(c.LogFile, os.O_CREATE, 0755) + if c.Log != nil { + reqDump, _ := httputil.DumpRequestOut(req, true) + respDump, _ := httputil.DumpResponse(resp, true) - logFile, err := os.OpenFile(c.LogFile, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0755) - if err == nil { - reqDump, _ := httputil.DumpRequestOut(req, true) - respDump, _ := httputil.DumpResponse(resp, true) + c.Log.Write([]byte("Request: " + string(reqDump) + "\nResponse: " + string(respDump) + "\n\n")) - logFile.WriteString("Request: " + string(reqDump) + "\nResponse: " + string(respDump) + "\n\n") - } } } diff --git a/types.go b/types.go index 987f942..d6d45d7 100644 --- a/types.go +++ b/types.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "time" + "io" ) const ( @@ -82,7 +83,7 @@ type ( ClientID string Secret string APIBase string - LogFile string // If user set log file name all requests will be logged there + Log io.Writer // If user set log file name all requests will be logged there Token *TokenResponse }