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.
This commit is contained in:
Anthony Poschen 2016-05-14 15:39:35 +10:00
parent 86a0467076
commit f6bd5b385e
2 changed files with 11 additions and 16 deletions

View File

@ -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)
logFile, err := os.OpenFile(c.LogFile, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0755)
if err == nil {
if c.Log != nil {
reqDump, _ := httputil.DumpRequestOut(req, true)
respDump, _ := httputil.DumpResponse(resp, true)
logFile.WriteString("Request: " + string(reqDump) + "\nResponse: " + string(respDump) + "\n\n")
}
c.Log.Write([]byte("Request: " + string(reqDump) + "\nResponse: " + string(respDump) + "\n\n"))
}
}

View File

@ -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
}